if its too loud, turn it down

Tuesday, June 2, 2009

Export MP3 files in an itunes playlist to your desktop

This script takes an itunes playlist exported as plain text (save to your mac desktop) and copies all the mp3s into a folder of the same name on your desktop helpful for burning data cds of mp3s. Just run the script and you'll be prompted for the necessary info.
#!/usr/bin/perl

use strict;
use File::Copy;

my $desktop = "/Users/YOU/Desktop/";

# prompt the user for the filename
my $file = &promptUser("Enter name of the plain-text itunes playlist on your desktop","");

# error checking
if (!$file) {
    print "Enter the name of the exported playlist on your desktop.\n";
    $file = &promptUser("Enter name of the plain-text itunes playlist on your desktop","");
}

# make the full path
my $file_quoted = quotemeta($file);
my $filepath = ${desktop}.${file};

# make sure the file exists
if (not(-f $filepath)) {
    print "File does not exist, please try again.\n";
    $file = &promptUser("Enter name of the plain-text itunes playlist on your desktop","");
}

# create the directory if it doesn't exist
my @tmp = split(/\./,$file);
my $fulldirname = ${desktop}.$tmp[0]." MP3s";
#print "$fulldirname\n";
if (not(-d $fulldirname)) {
    mkdir($fulldirname,0777) || die "Could not create directory on desktop: $!\n";
}

# loop thru the file - create a temp file that doesn't have \r's in it
open (FILE,"$filepath") || die "Cannot open $filepath : $! \n";
open (TMP, ">${desktop}/tmp.txt") || die "Cannot open ${desktop}/tmp.txt for writing : $! \n";
while (<FILE>) {
    my $line = $_;
    $line =~ s/\015/\n/g;
    print TMP "$line";
}
close(TMP);
close(FILE);

# loop thru our fixed file
my $newfile = "${desktop}/tmp.txt";
open (NEWFILE,"$newfile") || die "Cannot open $newfile : $! \n";
my $linect = 0;
while (<NEWFILE>) {
    chomp($_);
    my @column = split(/\t/, $_);
    my $track = $column[26];
    $track =~ s/:/\//g;
    $track = "/Volumes/" . $track;
    $track = &stripCrap($track);
    my $check = quotemeta($track);
    my $trackname = `/usr/bin/basename $check`;
    $trackname = &stripCrap($trackname);

    # copy the file to our directory
    if (-f $track) {
        print "copying $trackname...\n";
        my $newloc = ${fulldirname} . '/' . ${trackname};
        copy($track,$newloc) || die "Cannot copy $track : $! \n";
    }

    $linect++;
}
close (NEWFILE);

# remove our tempfile
unlink($newfile);

################# Subroutines #################

sub stripCrap {

    my $string = shift;
    $string =~ s/\n//g;
    $string =~ s/\r//g;
    $string =~ s/\r\n//g;
    $string =~ s/\012//g;
    $string =~ s/\015//g;
    $string =~ s/\012\015//g;

    return($string);
}

sub promptUser {

   my ($promptString,$defaultValue) = @_;
   if ($defaultValue) {
      print $promptString, "[", $defaultValue, "]: ";
   } else {
      print $promptString, ": ";
   }
   $| = 1;               # force a flush after our print
   $_ = <STDIN>;         # get the input from STDIN (presumably the keyboard)
   chomp;

   if ("$defaultValue") {
      return $_ ? $_ : $defaultValue;    # return $_ if it has a value
   } else {
      return $_;
   }

} # end sub promptuser

4 comments:

  1. One caution: you lost a few variables in angle brackets. Reading
    from STDIN is broken, as are your two while loops. I'll add the
    details below, although the items in angle brackets might get
    removed from this comment as well.

    Line 099 should be: $_ = &ltSTDIN&gr;
    (that's STDIN in angle brackets, after the equals sign)

    Line 038 should be: while (&ltFILE&gr) {
    (that's a FILE, in angle brackets, between the parentheses)

    Line 050 should be: while (&ltNEWFILE&gr) {
    (that's a NEWFILE, in angle brackets, between the parentheses)


    Thanks again for this script!


    (BTW, it wasn't at all easy to post this here. I detest signing
    into webpages, and spent 10 minutes trying to find an email address
    I could send it to instead. I almost gave up, except that I
    figured that the next person looking for this very helpful script
    might not know enough perl to realize the missing angle brackets. I ended up using an old worthless gmail account)

    ReplyDelete
  2. Thanks for pointing that out, I made the fix in the code. Blogger isn't the best for posting code, comments!

    ReplyDelete
  3. Hey Guy.. THANKS for this great script!

    running 10.5.8 though and copy/pasting this into 'script editor' I get

    suspected end of line, etc. but found identifier

    not the programming guy here..but just a tinkerer..:)

    ReplyDelete
  4. Hi - it's not an applescript, it's a PERL script.

    ReplyDelete

Note: Only a member of this blog may post a comment.