After working on the C# iTunes Now Playing program, I thought I’d give the Perl Win32::OLE
module a try.
This module is Activestate Perl‘s COM interface for Microsoft Windows.
After using Perl for nearly 8 years, I’ve never given it a try before, and it turns out to be surprisingly easy to use.
The following script works exactly the same way as the C# version.
Firstly we need to create a new iTunes.Application object.
my $iTunesApp = new Win32::OLE("iTunes.Application");
Next we get the current track and print the name and artist.
my $track = $iTunesApp->CurrentTrack;
print "Current Track: " . $track->Name . "nCurrent Artist: " . $track->Artist . "n"
And that’s it. Obviously we should be checking to make sure the objects were create correctly, but this is just a simple example, not live code.
So to recap, here’s the final working example code.
#!/usr/bin/perl
use strict;
use Win32::OLE;
my $iTunesApp = new Win32::OLE("iTunes.Application");
my $track = $iTunesApp->CurrentTrack;
print "Current Track: " . $track->Name . "nCurrent Artist: " . $track->Artist . "n"