#!perl -all #----------------------------------------------------------# # ipod.pl # v1.0 # # Script for accessing/controlling an iPod via various # functions provided by the iTunes SDK. # # Written by Bernie Zimmermann # Last modified on July 24, 2005 #----------------------------------------------------------# use strict; use Win32::OLE; # global variables my $a = 0; my $b = 0; my $c = 0; my $d = 0; my $e = 0; my ($aval, $bval, $cval, $dval, $eval); my $i; my $j; my $iPod; my $current; my $longest; my $tracks; my $track; # get a handle on iTunes my $iTunesApp = new Win32::OLE("iTunes.Application"); # get all the available sources my $sources = $iTunesApp->Sources; # loop through the sources looking for an iPod for ($i=1; $i<=$sources->Count; $i++) { # grab the current source $current = $sources->Item($i); # see if its an iPod if (2 == $current->Kind) { # it is so set the global iPod handle $iPod = $current; # and break the loop last; } } # start collection of high-level iPod data my $version = $iPod->SoftwareVersion(); my $capacity = $iPod->Capacity(); my $free = $iPod->FreeSpace(); # start collection of playlist data my $playlists = $iPod->Playlists(); my $playlistCount = $playlists->Count(); # get handle on largest playlist (assume it's the main one) for ($i=1; $i<=$playlistCount; $i++) { # get the current playlist $current = $playlists->Item($i); # if first time through or largest, save it if ($i == 1 || $current->Duration() > $longest->Duration()) { $longest = $current; } } # get all the tracks in the playlist $tracks = $longest->Tracks(); # loop through the tracks for ($j=1; $j<=$tracks->Count(); $j++) { # grab the current track $track = $tracks->Item($j); # see if it qualifies as most played MostPlayed(); } # print iPod data print "\niPod Analyzer"; print "-------------\n"; print "Software Version:\t$version"; print "Total Capacity:\t\t$capacity"; print "Free Space:\t\t$free\n"; print "Scanned Playlist:\t".$longest->Name()."\n"; print "Most Played Tracks:\n"; print "\t$aval\n\t$bval\n\t$cval\n\t$dval\n\t$eval\n"; print "Script by Bernie Zimmermann"; print "http://www.bernzilla.com/"; print "Copyright 2005"; #----------------------------------------------------------# # Helper Function(s) #----------------------------------------------------------# sub MostPlayed { # get its count my $count = $track->PlayedCount(); # see if it falls into any of the highest buckets if ($count > $a) { # throw this guy in the a bucket $a = $count; $aval = $track->Artist()." - ".$track->Name()." [".$count." plays]"; } elsif ($count > $b) { # throw this guy in the b bucket $b = $count; $bval = $track->Artist()." - ".$track->Name()." [".$count." plays]"; } elsif ($count > $c) { # throw this guy in the c bucket $c = $count; $cval = $track->Artist()." - ".$track->Name()." [".$count." plays]"; } elsif ($count > $d) { # throw this guy in the d bucket $d = $count; $dval = $track->Artist()." - ".$track->Name()." [".$count." plays]"; } elsif ($count > $e) { # throw this guy in the e bucket $e = $count; $eval = $track->Artist()." - ".$track->Name()." [".$count." plays]"; } else {} }