Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JScript: Find broken iTunes tracks

JScript to find broken tracks that iTunes can no longer find. Perfect for those who like to manage their large music libraries.

var fso, iTunesApp;
/* global WScript, ActiveXObject, Enumerator */
fso = new ActiveXObject("Scripting.FileSystemObject");
iTunesApp = new ActiveXObject("iTunes.Application");
outputResult(findBrokenTracks());
function findBrokenTracks () {
var trackKindFile = 1,
brokenTracks = [],
tracksEnum,
track;
tracksEnum = new Enumerator(iTunesApp.LibraryPlaylist.Tracks);
while (!tracksEnum.atEnd()) {
track = tracksEnum.item();
if (track.Kind == trackKindFile && track.Location === "") {
brokenTracks.push({
name: track.Name,
artist: track.Artist
});
}
tracksEnum.moveNext();
}
return brokenTracks;
}
function outputResult (tracks) {
var numBrokenTracks;
numBrokenTracks = tracks.length;
if (numBrokenTracks > 0) {
writeToFile(tracks);
}
displayPopup(numBrokenTracks);
}
function writeToFile (tracks) {
var currentDir, outputPath, outputFile, track;
currentDir = fso.GetAbsolutePathName(".");
outputPath = fso.BuildPath(currentDir, "tracks.txt");
outputFile = fso.CreateTextFile(outputPath, true, true);
for (var i=0; i<tracks.length; i++) {
track = tracks[i];
outputFile.WriteLine(track.name + " by " + track.artist);
}
outputFile.Close();
}
function displayPopup (numBrokenTracks) {
var message;
if (numBrokenTracks == 0) {
message = "No broken tracks were found!";
}
else if (numBrokenTracks == 1) {
message = "There is 1 broken track.";
}
else {
message = "There are " + numBrokenTracks + " broken tracks.";
}
WScript.Echo(message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment