# Play/Pause hotkey for Pandora and iTunes

Earlier today, I showed how to use AppleScript and GeekTool to display on the Desktop the track currently playing in either iTunes or PandoraBoy, a standalone program for playing the Pandora music service. I mentioned at the end of that post that I wanted to set up a single hotkey that would play or pause whichever program I was listening to. It turned out to be pretty simple.

Here’s the AppleScript that does the work:

 1:  -- find out what's running
2:  tell application "System Events"
3:   set numBoy to count (every process whose name is "PandoraBoy")
4:   set numTunes to count (every process whose name is "iTunes")
5:  end tell
6:
7:  (*
8:  This is the logic of the following:
9:     * if whatever is playing, pause it
10:     * if both are running but neither are playing, start playing PandoraBoy
11:     * if only one is running, flip its play/pause state
12:     * if neither are running, do nothing
13:  *)
14:
15:  if numBoy > 0 then
16:   if numTunes > 0 then -- both PandoraBoy and iTunes are running
17:     tell application "iTunes"
18:       if player state is playing then -- iTunes is playing
19:         pause
20:         tell application "PandoraBoy"
21:           if player state is playing then -- both PandoraBoy and iTunes are playing
22:             playpause
23:           end if
24:         end tell
25:       else -- iTunes is not playing
26:         tell application "PandoraBoy" to playpause
27:       end if
28:     end tell
29:   else -- PandoraBoy is running but iTunes is not
30:     tell application "PandoraBoy" to playpause
31:   end if
32:  else if numTunes > 0 then -- iTunes is running but PandoraBoy is not
33:   tell application "iTunes" to playpause
34:  end if


The script is named “MultiPlayPause.scpt,” and it’s saved in my Library/Scripts folder where Quicksilver can find and catalog it. On my work computer, I’ve made a Quicksilver trigger to run the script whenever the F13 key is pressed. I like F13 because it’s a big key that’s easy to find and doesn’t have a default action associated with it. On my iBook, which doesn’t have an F13, I have the trigger set to Command-F12.

The logic of the script is summarized in the block comment on lines 8-12. If only one of the two programs is running, the hotkey will flip it from paused to playing or vice versa. If both programs are running, the hotkey will usually flip the state of PandoraBoy. I made this choice because PandoraBoy’s only purpose is to play music; if it’s running, it’s probably because I want to listen to it. iTunes, on the other hand, could be running just because I’m ripping a CD or syncing my iPod.

There’s one exception to PandoraBoy’s precedence, and it’s the reason the script has so many if clauses: when music is playing, hitting the hotkey pauses it regardless of what’s running or what’s playing. When I’m listening at work and the phone rings, I want the hotkey to pause the music no matter what—even in the unusual case where both PandoraBoy and iTunes are running and I’m listening to iTunes. And even in the stupid case where both are running and both are playing.

Update
I should have mentioned that this script is working for me on both a G4 iBook with OSX 10.5.1 and an Intel iMac with OSX 10.4.11.