############################################################################### # # itunes_ipod_playlist.pl # # This script takes all the songs on the iPod and creates a new playlist in # iTunes called "iPod Songs" containing all the songs on the iPod. Tracks must # already be in your Library # # Copyright (C) 2007 Robert Jacobson # written by: Robert Jacobson (http://mysite.verizon.net/teridon/itunesscripts) # Last Updated: 28 Jan 2007 # Version 1.0 # # This script is GPL v2. see http://www.gnu.org/copyleft/gpl.html # ############################################################################### use File::Basename; my $PROGNAME = basename($0); my $VERSION = "1.0"; my $AUTHOR = "Robert Jacobson"; my $HOMEPAGE = "http://mysite.verizon.net/teridon/"; my $YEAR = 2007; my $GNU_URL = "http://www.gnu.org/copyleft/gpl.html"; { print "**************************************************************\n" . "$PROGNAME version $VERSION, Copyright (C) $YEAR $AUTHOR\n" . "Visit $HOMEPAGE for updates\n" . "$PROGNAME comes with ABSOLUTELY NO WARRANTY;\n". "This is free software, and you are welcome\n" . "to redistribute it under certain conditions\n" . "for details see $GNU_URL.\n" . "**************************************************************\n" . "\n" ; } use strict; use Win32::OLE; use Data::Dumper; ## Create the OLE Object my $iTunes = Win32::OLE->new('iTunes.Application') or die Win32::OLE->LastError(); # Get the possible sources my $sources = $iTunes->Sources(); my $sourcesCount = $sources->Count(); my $source = ''; my $sourceKind = ''; #Create a playlist my $arty_playlist = $iTunes->CreatePlaylist("iPod Songs"); my %seen; my $n = 1; # Get the "master" iPod playlist for ($n = 1; $n <= $sourcesCount; $n++) { $source = $sources->Item($n); $sourceKind = $source->Kind(); if ($sourceKind == 2) { print "iPod Source\n"; my $playlists = $source->Playlists(); my $num_playlists = $playlists->Count(); my $playlist = $playlists->Item(1); # Get all tracks on the iPod, and store the info in %seen with key "${name}${artist}${size}" my $tracks = $playlist->Tracks; my $num_tracks = $tracks->Count(); print "\t$num_tracks tracks\n"; # Get all the tracks in the playlist for (my $k = 1 ; $k <= $tracks->Count ; $k++ ) { my $track = $tracks->Item($k); my $artist = $track->Artist(); my $name = $track->Name(); my $size = $track->Size(); my $key = "${name} ${artist} ${size}"; $seen{$key} = 1; } } } # Get the iTunes Library playlist my $library = $iTunes->LibraryPlaylist(); my %found; # For every track in the Library list, generate the "key" my $tracks = $library->Tracks; my $num_tracks = $tracks->Count(); for (my $k = 1 ; $k <= $tracks->Count ; $k++ ) { my $track = $tracks->Item($k); my $artist = $track->Artist(); my $name = $track->Name(); my $size = $track->Size(); my $key = "${name} ${artist} ${size}"; # If seen{$key} then add track to new playlist if ($seen{$key}) { print "adding $key\n"; $arty_playlist->AddTrack($track); $found{$key} = 1; } } # Find tracks that were on iPod but couldn't find in the Library for my $key (sort keys %seen) { if (not $found{$key}) { print "couldn't find $key\n"; } } # Destroy the object. Otherwise zombie object will come back # to haunt you quit(); sub quit { # This destroys the object undef $iTunes; exit; }