tags:

views:

102

answers:

3

I've tried searching CPAN. I found Mac::iTunes, but not a way to assign a rating to a particular track.

A: 

Have a look at itunes-perl, it seems to be able to rate tracks.

mopoke
That script uses `Win32::OLE`, so it won't work on Macintosh.
Rob Kennedy
Oh. Yeah. That line where it says "iTunes for Windows". Dur.
mopoke
+3  A: 

You can write AppleScript to fully control iTunes, and there is a Perl binding Mac::AppleScript.

EDIT Code Sample:

use Mac::AppleScript qw(RunAppleScript);

RunAppleScript(qq(tell application "iTunes" \n set rating of current track to $r \n end tell));
Iamamac
Bill
+4  A: 

If you're not excited by Mac::AppleScript, which just takes a big blob of AppleScript text and runs it, you might prefer Mac::AppleScript::Glue, which provides a more object-oriented interface. Here's the equivalent to Iamamac's sample code:

#!/usr/bin/env perl
use Modern::Perl;
use Mac::AppleScript::Glue;
use Data::Dumper;

my $itunes = Mac::AppleScript::Glue::Application->new('iTunes');
# might crash if iTunes isn't playing anything yet
my $track = $itunes->current_track;
# for expository purposes, let's see what we're dealing with
say Dumper \$itunes, \$track;

say $track->rating; # initially undef
$track->set(rating => 100);
say $track->rating; # should print 100

All that module does is build a big blob of AppleScript, run it, and then break it all apart into another AppleScript expression that it can use on your next command. You can see that in the _ref value of the track object when you run the above script. Because al it's doing is pasting and parsing AppleScript, this module won't be any faster than any other AppleScript-based approach, but it does allow you to intersperse other Perl commands within your script, and it keeps your code looking a little more like Perl, for what that's worth.

Rob Kennedy