Trigger chains with AppleScript
Thursday, September 6th, 2007A while back, someone suggested the addition of an extra value to be used to retrieve results from an AppleScript. This is a fine idea and I’m going to take a close look at it in a future release, but the suggestion was made as a solution to a problem that could be solved by chaining triggers together using AppleScript.
Let me show you what I mean. Suppose you wanted a Hotkey that displayed track information for the current track in iTunes when pressed. You’d use a Hotkey Monitor trigger, which is simple enough. And you could get all of the information from an AppleScript task pretty easily:
tell application "iTunes"
set theName to name of current track
set theArtist to artist of current track
set theArtwork to null
if (count of artwork of current track) > 0 then
set theArtwork to data of artwork 1 of current track
end if
set theDescription to "Now playing:
" & theName & "
by
" & theArtist
end tellYou
But what to do with it? This is where the AppleScript Trigger comes in. Not only can this trigger be used to poke Proxi from iCal, Folder Actions, or anything else that can execute an AppleScript, but it can be used to have Proxi trigger itself. In this case, we want to pass this description and artwork back into Proxi and so we add the following to our script:
tell application "Proxi"
trigger description theDescription name "current track" pictImage theArtwork
end tell
theDescription and theArtwork are pretty self explanatory, but the name, “current track” is used by Proxi to distinguish one AppleScript trigger from another. So next we need to add an AppleScript trigger to Proxi and set the AppleScript trigger name to “current track”. Then we can add a Screen Message task and display the Image and Description values we got from the AppleScript trigger.
You can use this technique to string together as many triggers as you need. You could even use a dialog to prompt for information and put together a bit of logic by filtering on the return value. And yes you can create a recursive trigger if you want but it doesn’t take much to overflow the stack in AppleScript.
A blueprint demonstrating the current iTunes track hotkey I described above is available here.