A quick and dirty Node script to copy the files in an iTunes playlist to a folder, in random order and without preserving file names. I use it to copy music to my flashcard to listen to it in non-Apple hardware.
Getting the playlist
I generate my playlist in iTunes, then export them as XML by right clicking on the playlist name and selecting “Export…” from the contextual menu. At the end of this step I have an XML file which looks like
stream editor, another comand line tool that web developers don’t use enough IMHO. It comes by default with all Unix systems, including OS X. You give it (a) file(s), it applies text manipulations to it end you can save the results in another file. The manipulation themselves can be saved in a file…
…which is exactly what I am doing here, so I can reuse it later (see below)
this is where the output goes
The regular expression used is in a file (it makes developing it easier). The path to my music folder is hard coded. Note that regular expressions in the OS X version of sed are pretty basic, you don’t have “+” for example.
After that I am left a text file with a list of file paths in iTunes weird URL encoding variant.
chmod 775 music_from_itunes
I tried using the lazy modules to read the file as a stream instead of gobbling it up into an array in one go, but something is odd with my npm installation, and I didn’t have time to work out what. So I wrote it using only standard modules, plus one of mine. It will load the complete list in memory, shuffle it, and then print out a bash file to copy these shuffled files to an external hard disk. Note that all paths are hard coded at the top of the file.
The mandoline_helper module I wrote is a container for all my iTunes related scripts, it’s all rough and ready stuff focusing on functional style js.
At the end of this step I am left with a bash file, which is a collection of copy instructions.
cp "/Volumes/HD1T2/Music/Music/James Brown _ JB's/Hell/My Thang.mp3""/Volumes/SD/MUSIC/0001.mp3"cp "/Volumes/HD1T2/Music/Music/Cham/DJ Zhao_ Balmyard Rockers Mix/Funny Man.mp3""/Volumes/SD/MUSIC/0002.mp3"cp "/Volumes/HD1T2/Music/Music/Last Step/Sacred Symbols of Mu CD2/10 Lives With Angel.mp3""/Volumes/SD/MUSIC/0003.mp3"cp "/Volumes/HD1T2/Music/Music/Smokeringz/Professor Tsungs Art Of Science Funktion/10 Flytronix.mp3""/Volumes/SD/MUSIC/0004.mp3"cp "/Volumes/HD1T2/Music/Music/Compilations/Hip Hop Don't Stop - The Greatest/19 Egg Trippin'.mp3""/Volumes/SD/MUSIC/0005.mp3"...
Doing the actual copy
The output of the last step is a file in my home directory called bash_files.sh. I simply run that, and roughly one hour later (for a 16GB flash drive) it’s all done.
It (mostly) works
I run it on playlists of up to 5000 items and it worked ok, with only 1% of files that couldn’t be copied (weird character encoding introduced by iTunes that couldn’t be decoded easily). I can live with that.
Obviously there are lots of possible improvements, but I only had a window of a couple of hours and it was great just to get something that did the job.