O'Reilly logo

Mac OS X Hacks by Rael Dornfest, Kevin Hemenway

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required