/* SelectedLayersRenamer.jsx version .92 A Green Digital Entrustment (crgreen.com) */ var win = new Window('palette', 'Layer Rename',[300,100,645,362]); var w = buildUI(); if (w != null) { w.show(); } function buildUI() { if (win != null) { win.nameSearchLabel = win.add('statictext', [14,15,314,37], 'Search in Names:'); win.nameSearchT = win.add('edittext', [25,40,325,62], ''); win.nameReplaceLabel = win.add('statictext', [14,73,314,95], 'Replace with:'); win.nameReplaceT = win.add('edittext', [25,98,325,120], ''); win.typePnl = win.add('panel', [16,138,206,243], 'Rename Type:'); win.repRad = win.typePnl.add('radiobutton', [14,13,174,35], 'Search and Replace'); win.repRad.value = true; win.repRad.onClick = function () { doTextChange(win.nameSearchLabel, 'Search in Names:'); doTextChange(win.nameReplaceLabel, 'Replace with:'); }; win.appRad = win.typePnl.add('radiobutton', [14,39,174,61], 'Append'); win.appRad.onClick = function () { doTextChange(win.nameSearchLabel, 'Append Head with:'); doTextChange(win.nameReplaceLabel, 'Append Tail with:'); }; win.remRad = win.typePnl.add('radiobutton', [14,65,174,87], 'Remove # of Characters'); win.remRad.onClick = function () { doTextChange(win.nameSearchLabel, 'Remove this many chars from head (number):'); doTextChange(win.nameReplaceLabel, 'Remove this many chars from tail (number):'); }; win.okBtn = win.add('button', [240,220,320,242], 'OK', {name:'OK'}); win.okBtn.onClick = function () { doRenaming(this.parent); }; win.cancBtn = win.add('button', [240,185,320,207], 'Close', {name:'Cancel'}); win.cancBtn.onClick = function () {this.parent.close(1)}; } return win } function doTextChange(target, newText) { target.text = newText; } function doRenaming(theDialog) { // make sure a comp is selected var activeItem = app.project.activeItem; if (activeItem == null || !(activeItem instanceof CompItem)){ alert("You need to select some layers first."); } else { var s = activeItem.selectedLayers; var selNum = s.length; if (selNum < 2) { alert("You need to select at least two layers first."); } else { app.beginUndoGroup("layer rename"); var inputError = false; for (var n = (selNum-1); n >= 0; n--) { if ( ! inputError ) { item = s[n]; oldName = item.name; sear = theDialog.nameSearchT.text; repl = theDialog.nameReplaceT.text newName = oldName; if (theDialog.repRad.value) { //keep renaming until searchstring is no longer in name: while (newName.indexOf(sear) != -1) { newName = (newName.replace(sear, repl)); //names should not be over 25 chars long, so we trim if we need to } newName=(newName.substr(0,25)); } else if (theDialog.appRad.value) { newName=(sear + oldName + repl ); } else if (theDialog.remRad.value) { if (sear == "") {sear = 0;} if (repl == "") {repl = 0;} sear = ( parseInt(sear) ); repl = ( parseInt(repl) ); if ( (isNaN(sear)) || (isNaN(repl)) ) { alert('Error: Not a number?'); inputError = true; } else { newName=(newName.substr( sear, oldName.length-repl )); } } ////////////////////// item.name = newName; ////////////////////// } } app.endUndoGroup(); } } }