May 13, 2008 at 10:51 PM by Dr. Drang
Back in December I described an AppleScript that toggled either PandoraBoy or iTunes between play and pause. I used FastScripts to map it to the F13 key on my iMac at work, so I had just one button to press to stop the music when I had to take a call and restart it when the call was over.
Recently I’ve noticed a problem with the script when iTunes is the active music player. Whenever I attach my iPod or iPhone, the iTunes browser window shifts from the playlist I’m listening to and displays the summary information for the connected device. This shift happens even if iTunes is hidden, which it usually is. Now if I hit F13 to pause the music, the music stops; but when I hit F13 again, the music doesn’t restart because there are no songs in the iPod/iPhone summary view. To restart the music, I have to unhide iTunes, navigate to the playlist I was in, and click the Play button--which sort of defeats the purpose of having a single Play/Pause button.
I didn’t notice this problem when I first started using my script, because my habit then was to connect my iPod nano when I got to the office and not disconnect it until I left. Now I usually disconnect the nano sometime after it’s synced, and connect and disconnect the iPhone a couple of times during the day to make sure the phone’s calendar events and contacts list are up to date.
My solution was to add a couple of lines to the MultiPlayPause script. The lines are
set playingList to current playlist set view of front window to playingList
and they are added in the two locations in the script where iTunes gets paused. The effect is to force the iTunes browser to show the playlist I’m listening to, even if I’ve connected my iPod/iPhone.
The full MultiPlayPause script now looks like this:
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: set playingList to current playlist 20: set view of front window to playingList 21: pause 22: tell application "PandoraBoy" 23: if player state is playing then -- both PandoraBoy and iTunes are playing 24: playpause 25: end if 26: end tell 27: else -- iTunes is not playing 28: tell application "PandoraBoy" to playpause 29: end if 30: end tell 31: else -- PandoraBoy is running but iTunes is not 32: tell application "PandoraBoy" to playpause 33: end if 34: else if numTunes > 0 then -- iTunes is running but PandoraBoy is not 35: tell application "iTunes" 36: set playingList to current playlist 37: set view of front window to playingList 38: playpause 39: end tell 40: end if
I’m pretty sure this script still has some holes, but it seems to be working well so far.