Last active
August 29, 2015 14:15
-
-
Save dpoggi/4c3ef47f5d62c854ecba to your computer and use it in GitHub Desktop.
iTunes Playlist -> Ordered Folder Full of WAVs (for burning CDs on another machine)
This file contains 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
var options = { | |
"outputDir": "${HOME}/Downloads", | |
"numWorkers": 12, | |
}; | |
var App = Application.currentApplication(); | |
App.includeStandardAdditions = true; | |
var Finder = Application("Finder"); | |
var iTunes = Application("iTunes"); | |
var Terminal = Application("Terminal"); | |
(function (options) { | |
var failUnlessExists, sanitizeText, trackInfo, quoteArgs; | |
var lamePath, afPath, playlist, outputDir, commands, i, tracks, workerNum, track, extension, destination; | |
failUnlessExists = function (path) { | |
if (Finder.exists(Path(path))) { | |
return true; | |
} else { | |
throw "Couldn't find " + path.split('/').pop() + "!"; | |
return false; | |
} | |
}; | |
sanitizeText = function (text) { | |
return text.replace(/[^A-Za-z0-9 ]/g, "").replace(/[ ]{2,}/g, " "); | |
}; | |
trackInfo = function (i, track) { | |
var num; | |
num = i + 1 + ""; | |
if (num.length < 3) { | |
num = new Array(4 - num.length).join("0") + num; | |
} | |
return { | |
num: num, | |
artist: sanitizeText(track.artist()), | |
title: sanitizeText(track.name()), | |
path: track.location().toString().replace(/!/g, "\\!"), | |
}; | |
}; | |
quoteArgs = function () { | |
return "\"" + Array.prototype.slice.call(arguments).join("\" \"") + "\""; | |
}; | |
lamePath = "/usr/local/bin/lame"; | |
failUnlessExists(lamePath); | |
afPath = "/usr/bin/afconvert"; | |
failUnlessExists(afPath); | |
playlist = App.displayDialog("Please enter the name of the playlist you would like to prepare for CD:", { withTitle: "Playlist", defaultAnswer: "" }).textReturned; | |
outputDir = options.outputDir + "/" + sanitizeText(playlist); | |
Terminal.doScript(quoteArgs("mkdir", "-p", outputDir)); | |
commands = []; | |
for (i = 0; i < options.numWorkers; i++) { commands.push(""); } | |
tracks = iTunes.sources.byName("Library").userPlaylists.byName(playlist).tracks; | |
for (i = 0; i < tracks.length; i++) { | |
workerNum = i % options.numWorkers; | |
track = trackInfo(i, tracks[i]); | |
extension = track.path.split(".").pop(); | |
destination = outputDir + "/" + track.num + " " + track.artist + " - " + track.title + "." + extension; | |
commands[workerNum] += quoteArgs("cp", "-f", track.path, destination) + ";"; | |
if (extension === "mp3") { | |
commands[workerNum] += quoteArgs(lamePath, "--decode", destination) + ";"; | |
} else if (extension === "m4a") { | |
commands[workerNum] += quoteArgs(afPath, destination, "--verbose", "--data", "LEI16@44100", "--file", "WAVE", "--quality", "127") + ";"; | |
} | |
if (extension === "mp3" || extension === "m4a") { | |
commands[workerNum] += quoteArgs("rm", "-f", destination) + ";"; | |
} | |
} | |
for (i = 0; i < commands.length; i++) { Terminal.doScript(commands[i]); } | |
})(options); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment