Created
August 8, 2008 09:33
-
-
Save dann/4555 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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