Scripting iTunes with Perl - Part 1
Published July 6th, 2006 in Perl Tags: code, id3, itunes, perl, scripts, sdk, win32 ole.In this article I will explain how you can write Perl scripts which use the iTunes COM interface to control iTunes. You can download the iTunes COM SDK here. The SDK comes with excellent documentation and several example scripts. In this article we’ll be focusing on using the COM interface with the Win32::OLE Perl module.
The iTunes COM SDK was fist released with iTunes 4.5 and has been updated and enhanced with every major point release since. As I write this, the current COM interface version is 1.7, included in iTunes version 6.0.2.
The first script we that we’ll look at randomly selects tracks from your iTunes Library until it finds a track with artwork. When the script finds a track with artwork it will save it to a jpeg file.
-
use Win32::OLE;
First, we’ll include any necessary modules. Then we will create a new iTunes COM object and store it in the $iTunes variable. If the COM object cannot be created the script will exit with an error message.
-
my $tracks = $iTunes->LibraryPlaylist->Tracks;
-
my $numTracks = $tracks->Count;
Next, we’ll grab a reference to the entire collection of tracks in the iTunes library. The tracks collection also provides the Count method which will tell us how many tracks are in the collection, which we will call and store the result in $numTracks.
This code will randomly select a track from the collection and check to see if there is artwork in the tag. If any artwork is found it will be saved to C:\itunes_cover.jpg and the program will exit the while loop. [Note: the SaveArtworkToFile method requires an absolute path name.]
It’s a simple example, but we have learned how to use some of the various methods and properties exposed by the iTunes COM object. In the next article we’ll learn how to capture events from iTunes. Capturing events will allow your script to respond when a track begins or ends playing.
Download: itunes_demo_part_1.pl
WordPress database error: [Table 'cyberrazor_blog.wp_comments' doesn't exist]
SELECT * FROM wp_comments WHERE comment_post_ID = '6' AND comment_approved = '1' ORDER BY comment_date

No Responses to “Scripting iTunes with Perl - Part 1”
Please Wait
Leave a Reply