Control iTunes from the Command Line

cwest on 2002-09-25T18:58:58

Many thanks to brian d foy for making me more productive. No need to touch the mouse or switch applications or maximize iTunes. Now I can switch the song or replay without pain.

#!/usr/bin/perl

if ( my $action = shift ) { fork and exit; require Mac::iTunes; Mac::iTunes->controller->$action( @ARGV ); } else { print "Usage: $0 action [args]\n"; }


old skool

pudge on 2002-09-25T20:08:34

I've been doing this in iTunes since before there WAS an iTunes (that is, in SoundJam, its predecessor), from ircle. I have /next, /play, /stop, /previous, /np, etc., all as small compiled AppleScripts, which is quite a bit faster than compiling AppleScript on the fly.

brian may wish to look into including compiled AppleScripts with the module, or, better yet, using osacompile to write out scripts to a directory during make, or on demand. Something like:
my $folder = dirname($INC{'Mac/iTunes.pm');
my $scriptname = catname($folder, $unique_name_for_script);
_osacompile($script, $scriptname) unless -e $scriptname;
_osascript($scriptname);
Each execution could be significantly faster that way.

Anyway ... the IRC client is the OS!

Re:old skool

cwest on 2002-09-25T20:40:20

Yeah, Mac::iTunes is incredibly slow, thus my first ever direct use of fork in Perl code.

Re:old skool

brian_d_foy on 2002-09-25T21:06:10

i'm not too worried about speed at the moment. once Mac::iTunes::AppleScript loads, things are faster than i expected. if you use it for only one command then dump out of the script, well, that's what you get.

i'm also working on an iTunes command server so a daemon process accepts commands.

i'll check out the compiled stuff later. right now i'm still wrestling with iTunes to make it do what it advertises. :)

Re:old skool

cwest on 2002-09-25T21:12:06

Hrm.. POE::Component::(Client|Server)::iTunes

I'm feeling the itch...

Re:old skool

brian_d_foy on 2002-09-25T21:13:57

hey, i bet you're using the publically available Mac::iTunes, which is super slow because it doesn't use Mac::AppleScript for most things since Dan didn't have a way to return values at that time.

next week i'll release another version that gets rid all of the system calls. it's broken in some pretty heinous ways right now, but you can also scout around SourceForge if you really want to be brave. (the ticket to entry is actually finding it on SourceForge and figuring out anon CVS :)

Re:old skool

merlyn on 2002-09-26T00:13:49

Whoa. What we need is Inline::AppleScript. Ingy, please rescue us!