############################################################################### # # itunes_album_and_singles_playlist.pl # # This script will look in the entire music library and make two playlists - # "Albums" and "Singles". The "Albums" list will contain all the songs from # albums that have >= 6 tracks from that album. Similary, the "Singles" playlist # will have all the songs that have <= 3 tracks from that album. # # Copyright (C) 2007 Robert Jacobson # written by: Robert Jacobson (http://home.comcast.net/~teridon73/itunesscripts) # Last Updated: 05 Jun 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.1"; my $AUTHOR = "Robert Jacobson"; my $HOMEPAGE = "http://home.comcast.net/~teridon73/"; my $YEAR = 2007; my $GNU_URL = "http://www.gnu.org/copyleft/gpl.html"; { print "**************************************************************\n" . "$PROGNAME version $VERSION\n" . "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 a signal handler to destroy the iTunes object # in case our program quits before the end use sigtrap 'handler', \&quit, 'normal-signals'; ## Create the OLE Object my $iTunes = Win32::OLE->new('iTunes.Application') or die Win32::OLE->LastError(); my $Library = $iTunes->LibraryPlaylist(); my $AlbumsPlaylist = $iTunes->CreatePlaylist("Albums"); my $SinglesPlaylist = $iTunes->CreatePlaylist("Singles"); my $tracks = $Library->Tracks; my $num_tracks = $tracks->Count(); print "\t$num_tracks tracks\n"; my %seen; my %tracklist; # Get all the tracks in the playlist for (my $k = 1 ; $k <= $tracks->Count ; $k++ ) { my $track = $tracks->Item($k); my $track_kind = $track->Kind(); if ($track_kind == 1) { # FileOrCDTrack my $album = $track->album; #my $artist = $track->artist; if (length($album) > 0) { $seen{$album}++; push ( @{$tracklist{$album}}, $track ); } } } foreach my $key ( keys %seen) { if ( $seen{$key} >= 6 ) { foreach my $track ( @{ $tracklist{$key} } ) { $AlbumsPlaylist->AddTrack($track); } } if ( $seen{$key} <= 3 ) { foreach my $track ( @ { $tracklist{$key} } ) { $SinglesPlaylist->AddTrack($track); } } } # Destroy the object. Otherwise zombie object will come back # to haunt you quit(); sub quit { # This destroys the object undef $iTunes; exit; }