Controlling iTunes with Perl

The Mac::iTunes module means that controlling iTunes from across the room or across the world is only a Perl script away.

I created the Mac::iTunes Perl module to control iTunes from my scripts and from other machines. Everything that I present in this hack comes with either the Mac::iTunes or Apache::iTunes distribution (http://search.cpan.org/author/BDFOY/), available on the Comprehensive Perl Archive Network (CPAN).

Once I have a back end, I can create almost any interface to iTunes that I like — and I do.

iTunes Is AppleScriptable

Apple’s MP3 player, iTunes (http://www.apple.com/itunes/), has been AppleScript-aware since Version 2 (the latest version is 3.0.1). This gives me a lot of freedom to control how I use iTunes.

I can use Script Editor to create a script, but I can also use the osascript command-line tool from a Terminal window. I can use the -e switch to run a short script on the command line:

% osascript -e 'tell application "iTunes" activate'

Or, I can store the script in a file and pass it to the osascript on the command line:

-- iTunes script "quit_itunes"
-- run as "osascript quit_itunes"
tell application "iTunes"
quit
end tell
% osascript quit_itunes

Once I liberate myself from Script Editor, I have more flexibility.

Scripts for iTunes can automate a lot of my common tasks. Apple has a collection of scripts (http://www.apple.com/AppleScript/itunes/), and Doug’s AppleScripts for iTunes & SoundJam (http://www.malcolmadams.com/itunes/scrxcont.shtml ...

Get Mac OS X Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.