2015-04-22 19:33:53 +02:00
|
|
|
//
|
|
|
|
// This is an example script demonstrating how the renaming tool can be used.
|
|
|
|
//
|
|
|
|
|
2018-08-15 21:49:06 +02:00
|
|
|
//
|
2015-04-22 19:33:53 +02:00
|
|
|
// script configuration
|
2018-08-15 21:49:06 +02:00
|
|
|
//
|
2015-04-22 19:33:53 +02:00
|
|
|
|
|
|
|
// specifies the separator between artist, album and track number
|
|
|
|
var separator = ", ";
|
|
|
|
// specifies the separator between title and other fields
|
|
|
|
var lastSeparator = " - ";
|
|
|
|
// specifies whether the artist name should be included
|
2018-08-15 21:49:06 +02:00
|
|
|
var includeArtist = true;
|
2015-04-22 19:33:53 +02:00
|
|
|
// specifies whether the album name should be included
|
2018-08-15 21:49:06 +02:00
|
|
|
var includeAlbum = true;
|
2015-04-22 19:33:53 +02:00
|
|
|
// specifies whether the title should be included
|
|
|
|
var includeTitle = true;
|
2018-08-15 21:49:06 +02:00
|
|
|
// specifies the "distribution directory"
|
|
|
|
//var distDir = false; // don't move files around
|
|
|
|
var distDir = "/path/to/my/music-collection"; // move files to an appropriate subdirectory under this path
|
|
|
|
// directory used to store collections which contain songs from multiple artists
|
2016-03-03 22:21:56 +01:00
|
|
|
var collectionsDir = "collections";
|
2018-08-15 21:49:06 +02:00
|
|
|
// directory used to store miscellaneous songs by miscellaneous artists
|
|
|
|
var miscDir = "misc";
|
|
|
|
// directory used for miscellaneous songs by specific artist
|
|
|
|
var miscAlbumDir = "misc";
|
|
|
|
// condition to move files to miscDir
|
|
|
|
var isMiscFile = function (tag) { return tag.comment === "misc"; };
|
|
|
|
// condition to consider files part of a collection which contains songs from multiple artists
|
|
|
|
var isPartOfCollection = function (tag) { return tag.comment === "collection"; }
|
2015-04-22 19:33:53 +02:00
|
|
|
|
2018-08-15 21:49:06 +02:00
|
|
|
//
|
|
|
|
// helper functions
|
|
|
|
//
|
2015-04-22 19:33:53 +02:00
|
|
|
|
2018-08-15 21:49:06 +02:00
|
|
|
// returns whether the specified \a value is not undefined and not an empty string.
|
2015-04-22 19:33:53 +02:00
|
|
|
function notEmpty(value) {
|
|
|
|
return value !== undefined && value !== "";
|
|
|
|
}
|
2018-08-15 21:49:06 +02:00
|
|
|
// returns whether the specified \a value is not undefined and not zero.
|
2015-04-22 19:33:53 +02:00
|
|
|
function notNull(value) {
|
|
|
|
return value !== undefined && value !== 0;
|
|
|
|
}
|
2018-08-15 21:49:06 +02:00
|
|
|
// returns the string representation of \a pos using at least as many digits as \a total has
|
2015-04-22 19:33:53 +02:00
|
|
|
function appropriateDigitCount(pos, total) {
|
|
|
|
var res = pos + "";
|
|
|
|
var count = (total + "").length;
|
|
|
|
while (res.length < count) {
|
|
|
|
res = "0" + res;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2018-08-15 21:49:06 +02:00
|
|
|
// returns a copy of the specified \a name with characters that might be avoided in file names striped out
|
2015-04-22 19:33:53 +02:00
|
|
|
function validFileName(name) {
|
2018-08-15 21:49:06 +02:00
|
|
|
return name !== undefined ? name.replace(/[\/\\]/gi, " - ").replace(/[<>?!*|:\"\n\f\r]/gi, "") : "";
|
|
|
|
}
|
|
|
|
// returns a copy of the specified \a name with characters that might be avoided in directory names striped out.
|
|
|
|
function validDirectoryName(name) {
|
|
|
|
return name !== undefined ? name.replace(/[\/\\]/gi, " - ").replace(/[<>?!*|:\".\n\f\r]/gi, "") : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// actual script
|
|
|
|
//
|
|
|
|
|
|
|
|
// skip directories in this example script
|
|
|
|
if (!tageditor.isFile) {
|
|
|
|
tageditor.skip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse file using the built-in parseFileInfo function
|
|
|
|
var fileInfo = tageditor.parseFileInfo(tageditor.currentPath);
|
|
|
|
var tag = fileInfo.tag;
|
|
|
|
|
|
|
|
// deduce title and track number from the file name using the built-in parseFileName function (as fallback if tags missing)
|
|
|
|
var infoFromFileName = tageditor.parseFileName(fileInfo.currentBaseName);
|
|
|
|
|
|
|
|
// skip hidden and "desktop.ini" files
|
|
|
|
if (fileInfo.currentName.indexOf(".") === 0 || fileInfo.currentName === "desktop.ini") {
|
|
|
|
tageditor.skip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// skip files which don't contain audio or video tracks
|
|
|
|
if (!fileInfo.hasAudioTracks && !fileInfo.hasVideoTracks) {
|
|
|
|
tageditor.skip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter backup and temporary files by putting them in a separate directory
|
|
|
|
if (fileInfo.currentSuffix === "bak") {
|
|
|
|
tageditor.move("backups");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fileInfo.currentSuffix === "tmp") {
|
|
|
|
tageditor.move("temp");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// define an array for the fields to be joined later
|
|
|
|
var fields = [];
|
|
|
|
|
|
|
|
// get the artist, remove invalid characters and add it to fields array
|
|
|
|
var artist = validFileName(tag.artist);
|
|
|
|
if (includeArtist && !isPartOfCollection(tag) && notEmpty(artist)) {
|
|
|
|
fields.push(artist);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the album and remove invalid characters and add it to fields array
|
|
|
|
var album = validFileName(tag.album);
|
|
|
|
if (includeAlbum && notEmpty(tag.album)) {
|
|
|
|
fields.push(album);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the track/disk position and add it to fields array
|
|
|
|
// use the value from the tag if possible; otherwise the value deduced from the filename
|
|
|
|
if (notNull(tag.trackPos)) {
|
|
|
|
var pos = [];
|
|
|
|
// push the disk position
|
|
|
|
if (notNull(tag.diskPos) && notNull(tag.diskTotal) && tag.diskTotal >= 2) {
|
|
|
|
pos.push(appropriateDigitCount(tag.diskPos, tag.diskTotal));
|
|
|
|
}
|
|
|
|
// push the track count
|
|
|
|
if (notNull(tag.trackTotal)) {
|
|
|
|
pos.push(appropriateDigitCount(tag.trackPos, tag.trackTotal));
|
2015-04-22 19:33:53 +02:00
|
|
|
} else {
|
2018-08-15 21:49:06 +02:00
|
|
|
pos.push(appropriateDigitCount(tag.trackPos, 10));
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
2018-08-15 21:49:06 +02:00
|
|
|
fields.push(pos.join("-"));
|
|
|
|
} else if (notNull(infoFromFileName.trackPos)) {
|
|
|
|
fields.push(appropriateDigitCount(infoFromFileName.trackPos, 10));
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
|
2018-08-15 21:49:06 +02:00
|
|
|
// join the first part of the new name
|
|
|
|
var newName = fields.join(separator);
|
|
|
|
|
|
|
|
// get the title and append it
|
|
|
|
var title = validFileName(tag.title);
|
|
|
|
if (includeTitle) {
|
|
|
|
// use value from file name if the tag has no title information
|
|
|
|
if (!notEmpty(title)) {
|
|
|
|
title = validFileName(infoFromFileName.title);
|
|
|
|
}
|
|
|
|
if (newName.length > 0) {
|
|
|
|
newName = newName.concat(lastSeparator, title);
|
2015-04-22 19:33:53 +02:00
|
|
|
} else {
|
2018-08-15 21:49:06 +02:00
|
|
|
newName = newName.concat(title);
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-15 21:49:06 +02:00
|
|
|
// append an appropriate suffix
|
|
|
|
var suffix = "";
|
|
|
|
if (notEmpty(fileInfo.suitableSuffix)) {
|
|
|
|
// get a suitable suffix from the file info object if available
|
|
|
|
suffix = fileInfo.suitableSuffix;
|
|
|
|
} else if (notEmpty(fileInfo.currentSuffix)) {
|
|
|
|
// or just use the current suffix otherwise
|
|
|
|
suffix = fileInfo.currentSuffix;
|
|
|
|
}
|
|
|
|
if (notEmpty(suffix)) {
|
|
|
|
newName = newName.concat(".", suffix);
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply new name
|
|
|
|
tageditor.rename(newName);
|
|
|
|
|
|
|
|
// set the distribution directory
|
|
|
|
if (!distDir) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var path = [distDir];
|
|
|
|
var artist = validDirectoryName(tag.artist);
|
|
|
|
if (isPartOfCollection(tag)) {
|
|
|
|
path.push(collectionsDir);
|
|
|
|
} else if (isMiscFile(tag)) {
|
|
|
|
path.push(miscDir);
|
|
|
|
} else if (notEmpty(artist)) {
|
|
|
|
path.push(artist);
|
|
|
|
} else {
|
|
|
|
path.push(misc);
|
|
|
|
}
|
|
|
|
var album = validDirectoryName(tag.album);
|
|
|
|
if (notEmpty(album)) {
|
|
|
|
if (notEmpty(tag.year)) {
|
|
|
|
path.push([tag.year.split("-")[0], album].join(" - "));
|
2015-04-22 19:33:53 +02:00
|
|
|
} else {
|
2018-08-15 21:49:06 +02:00
|
|
|
path.push(album);
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
2018-08-15 21:49:06 +02:00
|
|
|
} else if (notEmpty(artist) && !isMiscFile(tag)) {
|
|
|
|
path.push(miscAlbumDir);
|
|
|
|
}
|
|
|
|
if (tag.diskTotal >= 2) {
|
|
|
|
path.push("Disk " + appropriateDigitCount(tag.diskPos, tag.diskTotal));
|
2015-04-22 19:33:53 +02:00
|
|
|
}
|
2018-08-15 21:49:06 +02:00
|
|
|
// apply new relative directory
|
|
|
|
tageditor.move(path.join("/"));
|