Hacking My Mac's Keyboard to Control Pandora

In this how-to, I’ll explain how I hacked my MacBook Pro’s built-in media keys to control the Pandora One desktop app. We’ll use AppleScript, Keyboard Maestro, Growl and, of course, an obscure utility posted to a French language forum. It took me a bit of hunting to piece together the essential bits, so I think you’ll find this how-to useful. 

Important Note: This hack is designed for how Snow Leopard (Mac OS X 10.6) handles the interaction between the media keys and iTunes. It goes without saying that I take no responsibility for however you may break your computer :)

Why Write Another Tutorial About This Problem? 

My frustration of not being able to control Pandora from my keyboard’s media keys recently bubbled to a head. I’d pondered the problem for months until the annoyance became prevalent enough that I had to solve the problem. Once I compiled and tweaked things a bit, I decided I’d share my experience here so the next person looking to do this has an easier time than I did.

The first Google search result I found useful was Nikolas Means’ excellent write-up and AppleScripts. In fact, without using Nikolas’s work as a basis, I don’t know where I’d be with this. You will notice a few parts of my code differs from his though, mostly because I encountered a problem he didn’t mention at the time: Snow Leopard always defaults to controlling iTunes if you have it open, and will even go so far as to open it if it’s not. Likewise, I removed the condition to control iTunes from his AppleScript.

I also addressed an issue I encountered with both iTunes and Pandora open together. I’ve added code to not pass the Play/Pause or Skip Song command to Pandora when both are open. This way, you don’t wind up playing music from both applications. If both are open, you’ll see a Growl notification to let you know that iTunes has assumed control of the media keys. (The official Growl documentation has a great tutorial on how to display notifications with AppleScript, which is where I found the code to start from.)

And believe it or not, finding a decent solution to iTunes starting up whenever the Play/Pause key is pressed actually took longer than any part of this hack. Fortunately, LifeHacker linked to a utility (posted on a French language forum, no less) that fixes this problem. This is by far the sketchiest piece of the whole hack, but so far it’s been working fine for me.

So, all that said, let’s get started. (It’s best to quit iTunes and the Pandora One desktop app at this point.)

Installation & Use:

1. First, download and install Keyboard Maestro. The trial version is free, but it does cost $36 to buy. (If anyone knows of a free (as in beer) equivalent, please let me know. You can also use USB Overdrive, but I personally prefer the featureful Maestro.)

2. Some optional Keyboard Maestro maintenance: Once you have the Maestro installed, run it and open up it’s preferences, then check the box for Start engine at login. (If you don’t know where its preferences are located, take this as an indicator that it’s best you stop this how-to right now!) Optionally, you may want to disable both of the two default groups. This is done by selecting the group in the left-most panel, clicking the Edit button along the bottom, then toggling the Enabled button in the top-right.

3. Add a new group by clicking the + button in the bottom-left. Name the group Pandora then de-select the Edit button along the bottom.

4. Add a new action by clicking the + button at the bottom of the middle panel. Name the script Play/Pause 

5. Click the + button for New Trigger, select Device Trigger, press the Play/Pause button on your keyboard, then uncheck the With Modifiers checkbox. Note: Quit iTunes if it starts up, which it should. This is fine, we’ll fix it later.

6. Click the + button for New Action. The left-most panel will reveal some choices. Select Execute, then double-click Execute an AppleScript from the middle panel.

7. In the box labeled Execute AppleScript that appears in the right-most panel, paste in this AppleScript code (Plain-Text).

8. Repeat steps 4-7, but use the keyboard’s Next Track button, name the action Skip Song and use this AppleScript code (Plain-Text).

Now, if you start up the Pandora One desktop app, you may notice you can use your keyboard’s media keys to pause and resume the music and to skip the currently playing song. However, iTunes will start up right away. So finally, let’s fix that next…

9. Download and run this utility featured on LifeHacker. (If you ever want the iTunes default functionality back, just run the utility again.)

And that’s it! You should now be able to control Pandora with your keyboard’s media keys. I’ve installed this hack on both my MacBook Pro and my iMac, which has a Logitech DiNovo Edge keyboard connected. Both have been working great for me over the past few days.

I hope you find this tutorial useful. If you have any suggestions and/or feedback, please feel free to send them my way.

Issues & Updates:

So far I’ve encountered the following 3 issues, all of which are fairly minor:

1. The AppleScript can take a second or two to compile, so you may notice a delay between pressing the key and the action happening.

2. Pressing the keyboard keys brings the Pandora app into focus in-front of other running apps

3. You can’t minimize the Pandora app because the AppleScripts can’t send the necessary key presses to it

As far as I can tell, #2 and #3 could be alleviated if a Pandora had a menu in the menu bar named “Control” with choices for Play/Pause, Skip Song, etc. If such a menu existed, AppleScript could access the menu items directly, instead of requiring key presses be scripted to the application.