Tuesday, January 2, 2007

Control iTunes over SSH

So I was looking for a good way to control iTunes running on my Mini mac at home. There's some payware options, like netTunes and webRemote, and some freeware options, like zTunes and iTRC. I kind of prefer using open source if I can get it, so I am giving the payware options a miss for now. Unfortunately none of the freeware options are cross-platform and secure at the same time. On top of that, most of the them are quite buggy. So while I wait for zTunes to become faster, less buggy and more secure, I chose the third option, which is Write Your Own :-) Thanks to the wonders of Applescript, iTunes is very accessible from other programs. You can basically get information about anything in your library and perform almost all the actions that you can perform from the program itself. So I wrote a remote control in bash, to be accessed over SSH. I present to you iTunesCLI 1.0. To use, save this script where you can find it again on the system that you want to use it from, for example in your home directory or in /usr/bin. Make it executable with chmod +x Then ssh to that system and run the script:
ssh -t mysystem ./iTunesCLI (when stored in your home directory)
ssh -t mysystem iTunesCLI (when stored in /usr/bin)
The -t option is needed to make keystrokes be recognized immediately. SSH tips:
  • Use an SSH agent so you don't have to type your password every time. For OS X, try for example SSH Keychain
  • The hostname for the remote machine is most likely machinename.local. For example, my Mac Mini is hooked up to the stereo and is called mini-me. I just run "ssh -t mini-me.local iTunesCLI".
I had to come up with some nifty tricks to make it fast and pretty. Look at the script file to see bash arrays, IFS swizzling, printf builtins, tput abuse, multiline commandlines embedded in backticks and a mangled model-view-controller concept, oh my! I sprinkled it with comments in the hopes of making it maintainable :-) Enjoy!


Dominik said...

Thanks for this. Does the trick. Would be nice to have a song browser :-)

vockleya said...

Thanks for this great program. It is just what I was looking for. I have made some major changes to the code and added a lot of new features. Take a look over at http://code.google.com/p/itunescli/ to see the newest version. (I made a Google Code project for this so others can easily upload updates)

Wout Mertens said...

vockleya, thanks for doing that! Awesome.