Pause the music in iTunes/Spotify with the keyboard
22 Jun 2011 - applescript, itunes, spotify
I've updated this text since the last version of Spotify supports AppleScript! So it's even easier now

Apple recently released new, very thin, keyboards. I love these low keys and I would never trade back. But there is one thing that bothers me with them. They have built in buttons for iTunes - F7, F8 and F9 are special buttons that controls iTunes playback. That would be perfectly allright unless you had to actually turn off the F-key functionality completely, or use the badly placed Fn-key borrowed from laptop keyboards.
Problem is, I use the F-keys extensively, for Dashboard, Exposé and F1/F2 is back/forward in my browsers and in the Finder, and I can't live without that.
So, how do you go about solving this, then? Well, my solution is to levae the F-keys as F-keys and use Quicksilver to attach special script to system-wide hot keys. There are other utilities for enabling system with hot keys as well, but I use Quicksilver.
Play/pause
tellapplication "iTunes" to playpause
The above script will play and pause music in iTunes. Easy enough, but since a while back, I use Spotify extensively, and it would be a shame if this script launches iTunes when i want to pause the music in Spotify. Therefor, I need a new script:
iTunes/Spotify
tellapplication "System Events" set aApps to (name of every process) end tell if "Spotify" is in aApps then tell application "Spotify" to playpause else if "iTunes" is in aApps then tell application "iTunes" to playpause end if

Ok, to walk you through it. First, I ask the system what applications are running, and then I check if Spotify is among the. If it is, I'm assuming that I am listening to music via Spotify. If Spotify isn't running and iTunes is, we play/pause it instead.
That's for play/pause, we need some more scripts for next/previous tack and volume up/down:
Next track
tellapplication "System Events" set aApps to (name of every process) end tell if "Spotify" is in aApps then tell application "Spotify" to next track else if "iTunes" is in aApps then tell application "iTunes" if player state is playing then next track end if end tell end if
Previous track
tellapplication "System Events" set aApps to (name of every process) end tell if "Spotify" is in aApps then tell application "Spotify" to previous track else if "iTunes" is in aApps then tell application "iTunes" if player state is playing then previous track end if end tell end if
Volume up
setgetSound to (get volume settings) as list set Theoutput to item 1 of getSound set volume output volume Theoutput + 10
Volume down
setgetSound to (get volume settings) as list set Theoutput to item 1 of getSound set volume output volume Theoutput - 10
And that's all! Have fun!














