Sam's Blog

Controlling iTunes from the command-line: bash-itunes

Date: Friday, 8 June 2012, 16:44.

Categories: bash-itunes, itunes, cli, bash, shell-scripting, apple-script.

A year or so ago I got myself a sound mixer, letting me mix the sound from all my computers into a single set of headphones and speakers. I'd been meaning to do it for years, and eventually settled on a Behringer XENYX 1002. It's a great little bit of kit, ideal for grabbing the sound from four machines and isn't too huge.

Of course, this then left me with another problem.

I tend to just play music on iTunes on my old laptop, and that means I have to physically use the old laptop if I want to skip tracks or adjust the volume, or mute, or whatever.

That's not a big deal when I'm sat at the desk, but a bit more annoying if I'm sitting back with my feet on the desk and my work laptop in my lap, comfortable and living my rockstar-developer lifestyle - leaning forwards is such a concentration-breaker.

So, uh, yeah, whatever. I found a need for controlling iTunes from the command-line over SSH. I had a look around at the existing command-line clients for iTunes and didn't really like most of them and decided to write my own.

Installation is simple: drop the script in your path on the machine with iTunes, ensure it's had chmod a+x itunes done appropriately and then just itunes help to see the instructions:

Navigation commands: play Start or resume playing. pause Pause playing. stop Stop playing. next Skip to next track. prev Skip back to previous track. info View info about current track. shuffle View or set shuffle status. repeat View or set repeat status. Volume commands: vol[ume] View or adjust iTunes volume 'up', 'down' or percentage. mute Mutes iTunes. unmute Unmutes iTunes, restoring previous volume level. sysvol[ume] View or adjust system volume 'up', 'down' or percentage. sysmute Mute system sound. sysunmute Unmute system sound. Application commands: open Start iTunes running. quit Quit iTunes. show Hide the iTunes window. hide Show the iTunes window. Additional commands: version Show version information and exit. help Show this help and exit.

There's a bunch of features I haven't added, it was only a quick hack of a couple of hours, I'll add more when I have time.

If you're wondering about the SSH part, I just drop a second script on other machines as ~/bin/itunes:

ssh itunesmachine "itunes $*"

I should probably double check the escaping on that, might want it to be something like ssh itunesmachine itunes "$@" instead, but I haven't needed to worry about it yet.

I also want to give a big thanks to Apple for making the effort to make iTunes so scriptable from AppleScript, it's one of those little things that they didn't have to do, but that makes life so much easier when you need it.

Browse Sam's Blog Subscribe to Sam's Blog

By day of June: 07, 08, 15.

By month of 2012: April, May, June, July, December.

By year: 2010, 2011, 2012, 2013.

Or by: category or series.


blog comments powered by Disqus
© 2009-2013 Sam Graham, unless otherwise noted. All rights reserved.