############################################################################### # # itunes_add_m3u_playlist.pl # # This script will find all .m3u playlists in the given directory and add the # contents to iTunes. # # 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; use File::Basename; use File::Find; sub wanted; ## Create the OLE Object my $iTunes = Win32::OLE->new('iTunes.Application') or die Win32::OLE->LastError(); print "Enter path of directories containing m3u playlists: "; chomp (my $dirpath = ); print "Recurse into subdirectories? (y/n) "; chomp (my $answer = ); my $recurse = 0; if ($answer =~ /^y/i) { $recurse = 1; } if ($recurse) { find(\&wanted, $dirpath); } else { opendir(DIR, $dirpath) or die "could not open directory $dirpath : $!"; my $m3u_base; for $m3u_base ( grep { /\.m3u$/ && -f "$dirpath/$_" } readdir(DIR) ) { print "base is $m3u_base\n"; #Create a playlist my $new_playlist_name = $m3u_base; $new_playlist_name =~ s/\.m3u$//; print "name is $new_playlist_name\tbase is: $m3u_base\n"; my $new_playlist = $iTunes->CreatePlaylist("$new_playlist_name"); my $m3u_path = "$dirpath\\$m3u_base"; my $operationStatus = $new_playlist->AddFile($m3u_path); sleep 1 while $operationStatus->InProgress; } } quit(); sub quit { # Destroy the object. Otherwise zombie objects will come back # to haunt you # NOTE: You can also free the memory by exiting the application undef $iTunes; exit; } sub wanted { my $m3u_base = $_; return unless (-f $m3u_base && $m3u_base =~ /\.m3u$/); my $m3u_path = $File::Find::name; print "base is $m3u_base\n"; print "path is $m3u_path\n"; #Create a playlist my $new_playlist_name = $m3u_base; $new_playlist_name =~ s/\.m3u$//; print "name is $new_playlist_name\tbase is: $m3u_base\n"; my $new_playlist = $iTunes->CreatePlaylist("$new_playlist_name"); my $operationStatus = $new_playlist->AddFile($m3u_path); sleep 1 while $operationStatus->InProgress; }