How to select & rename tracks with illegal characters
Hi,
Looking for help in selecting tracks (not clips) that contain illegal characters (as listed by ProTools) and the replacing them with a simple "-".
The characters ProTools consider illegal are listed here:
https://avid.secure.force.com/pkb/articles/en_US/troubleshooting/What-characters-are-illegal-in-Pro-Tools
So far I've got the script scanning and selecting the tracks with the illegal characters I've specified as wildcards, the next phase would be replacing any illegal characters with a simple dash "-"
I currently have a long clunky script which recalls batch rename, but I know poking around here something beginning with the following script should be a start. Would love any help!
function selectTracksByNameWithWildcard(trackNames = []) {
function matchWildcard(str, rule) {
var escapeRegex = (str) => str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
return new RegExp("^" + rule.split("*").map(escapeRegex).join(".*") + "$", "i").test(str);
}
var allNames = sf.ui.proTools.trackNames;
var namesToSelect = allNames.filter(n => trackNames.some(tn => matchWildcard(n, tn)));
sf.ui.proTools.trackSelectByName({
names: namesToSelect,
deselectOthers: true,
});
}
selectTracksByNameWithWildcard([
'*|*',
'*"*',
'*:*',
'*<*',
'*>*',
'*?*',
'*/*',
'*\"*',
]);
Thanks!
- Dustin Harris @Dustin_Harris
There is likely a much more elegant way than this, but this is my approach :)
function renameTracksWithIllegalCharacters() { const charactersToReplaceAsRegex = [/\?/, />/, /"/, /\//, /\\/, /</, /\|/, /:/]; const tracksToRename = sf.ui.proTools.invalidate().trackGetAllTracks().names.filter(trackName => { let hasIllegalCharacter = false; charactersToReplaceAsRegex.forEach(char => { if (char.test(trackName)) hasIllegalCharacter = true }) return hasIllegalCharacter }) if (tracksToRename.length == 0) { log('No Illegal Characters Found'); throw 0; } tracksToRename.forEach(trackName => { sf.ui.proTools.invalidate().trackSelectByName({ names: [trackName], deselectOthers: true, }) let newName = trackName; charactersToReplaceAsRegex.forEach(char => { const regex = new RegExp(char, 'g') newName = newName.replace(regex, "-") }) sf.ui.proTools.selectedTrack.trackScrollToView(); sf.ui.proTools.selectedTrack.trackRename({ newName: newName }) }); } function main() { const consent = confirm('Rename tracks with illegal characters?'); if (!consent) { log('Cancelled'); throw 0; } /* MacOS thread scheduling mitigation */ sf.wait({intervalMs: 30}) sf.ui.proTools.appActivateMainWindow(); let originalTrackSelection = sf.ui.proTools.selectedTrackNames.slice() renameTracksWithIllegalCharacters(); sf.ui.proTools.mainWindow.invalidate(); if (originalTrackSelection.length > 0) { let track = sf.ui.proTools.trackGetByName({ name: originalTrackSelection[0] }).track if (track) track.trackScrollToView(); sf.ui.proTools.trackSelectByName({ names: originalTrackSelection, deselectOthers: true }) } } main();
- TTristan Hoogland @Tristan
Dustin it's only 9am on a Monday and you are my hero for the week!
Thanks so much! And FTR I think it's pretty elegant ;)