In my previous article, Scripting iTunes with Perl - Part 1, I demonstrated how the iTunes COM object can be used with Perl. In this article I will be showing you how to catch events from iTunes. The example script below catches the ‘OnPlayerPlayEvent’ and then displays the cover art from the current track in a Tk window.

As a side note, I have written a script which does a little bit more. After saving the image it is manipulated with GD and layered between two special PNG images (like “layers” in Photoshop). Then the final image is uploaded to my personal blog. I have omitted most of that code and leave this as an exercise for the reader.

  1. use strict;
  2. use Win32::OLE;
  3. use Tk;
  4. use Tk::JPEG;
  5.  
  6. my $iTunes_OLE;
  7. my $tkWin;
  8. my $tkPhoto;
  9. my $tmpFile = ‘C:\image.jpg’;

Include our necessary modules and define some global variables.

  1. sub cleanup {
  2.    undef $iTunes_OLE;
  3.    Win32::OLE->FreeUnusedLibraries();
  4.    exit(0);
  5. };
  6.  
  7. use sigtrap ‘handler’ => \&cleanup, ‘INT’;

The cleanup sub is called when an INT signal is trapped and will make sure our script exits gracefully.

  1. sub iTunesEvent {
  2.    my ($iTunes, $event, @args) = @_;
  3.    my $currTrack = $iTunes->CurrentTrack;
  4.  
  5.    if ( defined($currTrack) &&
  6.       ($event eq ‘OnPlayerPlayEvent’) &&
  7.       ($currTrack->Artwork->Count > 0) ) {
  8.  
  9.       my $coverArt = $currTrack->Artwork->Item(1);
  10.       $coverArt->SaveArtworkToFile($tmpFile);
  11.       my $tmpPhoto = $tkWin->Photo(‘img’, -file => $tmpFile);
  12.       $tkPhoto->destroy;
  13.       $tkPhoto = $tkWin->Label(-image => $tmpPhoto)->pack;
  14.    }
  15. }

This subroutine is the event handler for our iTunes COM object. It is called when an event is caught from iTunes. Line #5 checks if the CurrentTrack is defined and that the CurrentTrack has at least one image. It also checks to see if the event caught is the ‘OnPlayerPlayEvent’. If these conditions are true then it saves the artwork and displays it in a Tk window.

  1. $tkWin = MainWindow->new;
  2. $tkWin->title(“iTunes Artwork”);
  3. $tkPhoto = $tkWin->Label(-text => “Play some music!”)->pack;

This creates our Tk window which let’s us view the album artwork after it has been saved.

  1. Win32::OLE->Initialize(Win32::OLE::COINIT_OLEINITIALIZE);
  2. $iTunes_OLE = Win32::OLE->new(“iTunes.Application”) or die $!;
  3. Win32::OLE->WithEvents($iTunes_OLE, \&iTunesEvent);

This initializes OLE and turns on event handling. Event handling is not considered stable in the most recent libwin32 release. You can read more about it’s quirks here.

  1. $tkWin->waitVariable(\$iTunes_OLE);
  2. MainLoop;
  3. Win32::OLE->MessageLoop();
  4. cleanup;

This code attempts to keep the Tk and OLE loops in synch. First, Tk is told to include a non-blocking wait in it’s main loop which checks the iTunes_OLE variable for a change. Then, the loops are started in sequence. Finally, the cleanup handler is called in the case that an INT signal is not caught before both loops exit.

It should be apparent that there are many neat things you can do with the iTune COM interface, and as Apple continues to enhance the interface even more possibilities will exist!

Download: itunes_demo_part_2.pl

WordPress database error: [Table 'cyberrazor_blog.wp_comments' doesn't exist]
SELECT * FROM wp_comments WHERE comment_post_ID = '7' AND comment_approved = '1' ORDER BY comment_date


No Responses to “Scripting iTunes with Perl - Part 2”  

  1. No Comments

Leave a Reply

WordPress database error: [Table 'cyberrazor_blog.wp_comments' doesn't exist]
DESC wp_comments


Warning: Invalid argument supplied for foreach() in /home/.flopsey/cyberrazor/cyberrazor.com/wp-content/plugins/subscribe-to-comments.php on line 713

WordPress database error: [Table 'cyberrazor_blog.wp_comments' doesn't exist]
ALTER TABLE wp_comments ADD COLUMN comment_subscribe enum('Y','N') NOT NULL default 'N'