Jump to content

Pausing iTunes


Recommended Posts

Having just purchased the Powerpack, I am now learning how to create workflows.

I have downloaded several examples just to see how Alfred works.

This is my first attempt.

My Alfred workflow is set up with a keyword 'pause' which triggers this script.

on alfred_script(q)
tell application "iTunes"
pause
delay 10
play
end tell
end alfred_script

It just pauses the song for 10 seconds then resumes.

The only way I can change the duration is in the delay.

I want to be able to have 'pause nn' so that nn is the duration.

Is there a way to do this?

 

Thanks

Link to comment

Okay now. Sifted through a lot of examples trying to get some clues.

Changed the code to 

my alfred_script("{query}")
on alfred_script(q)
tell application "iTunes"
pause
delay q
play
end tell
end alfred_script

Now it works. If I remove the first line, it does not work. What does the first line mean?

 

I did not understand what to do with the argument following the keyword, now I do so I have learnt something.

 

EDIT: Where can I find out what {query} does and where it comes  from? Cannot find any reference to it.

Edited by Colin
Link to comment

I'm not sure if there is a specific explanation of {query}.
 
Essentially, {query} is a text macro used by Alfred to pass input to your scripts. When it runs your code, Alfred first replaces all occurrences of {query} with whatever the input to the script is. The input may be text entered by the user in Alfred's query box, the argument from a Script Filter result, or the STDOUT output from another script (i.e. what the script outputs with echo or print).
 
The only exception is Run NSAppleScript. Because these are loaded and run within Alfred, it can give you the input as an actual variable (instead of doing search/replace on your source code before running it), which is the q in on alfred_script(q)
 
So in your example, you don't need {query}. If you're running the script with a Run NSAppleScript Action, put your code in the on alfred_script(q) … end alfred_script function and q is your query.
 
However, if you're using a normal Run Script Action (or a Script Filter), these are run outside Alfred by whatever program you specify, and you need to use the set theQuery to "{query}" text macro to get the input.
 
on alfred_script(q) is a special function that is called when you run an AppleScript with Alfred's Run NSAppleScript Action. The equivalent function when run any other way (in Script Editor, Automator, with osascript etc.) is run()
 
So, the following version of your code will run either as a Script Filter/Run Script (where run() is called) or as an NSAppleScript (where alfred_script() is called): 

on run (argv)
	set theDelay to "{query}" as integer
	return my pauseITunes(theDelay)
end run

on alfred_script(q)
	set theDelay to q as integer
	return my pauseITunes(theDelay)
end alfred_script

on pauseITunes(theDelay)
	tell application "iTunes"
		pause
		delay theDelay
		play
	end tell
end pauseITunes
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...