[ rexroof.com ] [ biking ] [ blog ] [ work ] [ disposable ] [ crap ] [ code ] [ phone ] [ pictures ] [ yank ] [ url ] [ linklog ]

Thursday, February 10, 2005

pladd

I've written this script a half-dozen times at least, but this is the first one that doesn't use Mac::iTunes to create the playlist. I needed something that would add a directory of mp3s to a playlist in itunes with the same name as the directory, so I wrote this:


#!/usr/bin/perl
use strict; $|++;
use warnings;
use Mac::Glue qw(:all);
my $itunes = Mac::Glue->new("iTunes");

my $dir = shift;
die "specify directory on command line\n" unless ( $dir and -d $dir );
my $playlist = $dir;
$playlist =~ s/\///g;
$playlist =~ s/\s+/_/g;

die "no playlist found\n" unless ($playlist =~ m/[a-zA-Z]/);

# check the playlist total time to see if the playlist exists already.
# $pl_time will be null if playlist doesn't exist, "0:00" for empty playlist
my $pl_time = $itunes->obj(playlist=>$playlist)->prop('time')->get;
if ($pl_time) {
print "playlist exists!\n";
} else {
$itunes->make(new => 'playlist', with_properties => { name => $playlist });
}

opendir DIR, $dir or die $!;
# only adding songs m4a, m4b, and mp3s.
my @file_list = grep { /(mp3|m4[ab])$/i } readdir (DIR);

foreach my $file (@file_list) {
print $file, $/;
# add files to playlist.
$itunes->add("$dir/$file", to => $itunes->obj(playlist=>$playlist));
# remove original
unlink "$dir/$file" or warn $!;
}



you can also find it on the code section of my webpage here.

0 Comments:

Post a Comment

<< Home