Skip to content

Instantly share code, notes, and snippets.

@dann
Created August 8, 2008 09:33
Show Gist options
  • Select an option

  • Save dann/4555 to your computer and use it in GitHub Desktop.

Select an option

Save dann/4555 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Mac::iTunes;
use File::Find::Rule;
use Path::Class;
use Cwd;
use Perl6::Say;
our $itunes;
main();
sub main {
my $playlist_name = $ARGV[0] || "myfavorites";
my $songs_dir = $ARGV[1] || getcwd;
setup_itunes();
create_playlist($playlist_name);
my $songs = find_songs($songs_dir);
add_to_playlist( $playlist_name, $songs );
}
sub setup_itunes {
$itunes = Mac::iTunes->controller;
$itunes->activate;
}
sub create_playlist {
my $playlist_name = shift;
if ( $itunes->playlist_exists($playlist_name) ) {
say "$playlist_name already exists\n";
return;
}
$itunes->add_playlist($playlist_name);
}
sub find_songs {
my $songs_dir = shift;
my @files = File::Find::Rule->file()->name('*.mp3')->in($songs_dir);
\@files;
}
sub add_to_playlist {
my ( $playlist_name, $song_files ) = @_;
foreach my $song_file ( @{$song_files} ) {
$itunes->add_track( $song_file, $playlist_name );
}
}
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment