Controlling MPD with Mac Media Keys
I use a Macbook Pro for my work at Booking, and I use music (or sometimes background noise) to avoid distractions while at work. However, I'm not a terribly big fan of iTunes; I much prefer the simplicity of MPD. My client of choice for MPD is Practical Music Search (pmus), which is curses-based and provides a nice shuffle feature. However, I want to avoid going back-and-forth between pmus and my work when I want to pause or skip tracks, so I managed to repurpose OS X's media keys to manipulate MPD and pmus. Here's how I did it.
Remove default bindings
By default, OS X responds to the media keys by starting up iTunes (if it's not running) and forwarding the command on to iTunes. Removing this default behavior isn't obvious, but once you discover the solution, it's quite easy. I ended up using KeyRemap4MacBook; it requires a reboot after installation, but after it's done installing, you only need to open it in System Preferences and tell it to remove the behavior for the media keys.
Adding Applescript to manipulate pmus
Unfortunately, OS X doesn't allow you (to my knowledge) to execute arbitrary command lines when you press a key. Fortunately, Quicksilver does, at least in the form of Applescript. So I saved the following snippets to their own Applescript files:
do shell script "/usr/local/bin/tmux send-keys -t pmus l"
do shell script "/usr/local/bin/tmux send-keys -t pmus h"
do shell script "/usr/local/bin/mpc toggle"
The scripts for the next/previous song may look a little different than expected; it's because for the next song, I often rely on pmus' shuffle feature. Simply sending the 'next' command to MPD would not populate the next song from the shuffle, so I needed to use a workaround using tmux. This also means I need to run pmus from within a tmux session named 'pmus'.
======= Creating keybindings for the scripts ======
Now that those are done, open Quicksilver's preferences, go to the 'Triggers' tab, and add a hotkey for each script. You can simply drag and drop the script from Finder into Quicksilver for this.
Enjoy!
That was the last step! Enjoy your pmus-bound media keys!
-Rob
Published on 2012-07-26