The following script will probably be easier to use if you don't know regular expressions:
/*
Rename Selected Layers.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/trouble-with-photoshop-layer-masks/td-p/12698065
Stephen Marsh, 25th January 2022 - v1.0
*/
#target photoshop
function main() {
var promptString = prompt("New layer name:", "");
// Optionally select all layers, uncomment if required
//app.runMenuItem(stringIDToTypeID('selectAllLayers'));
/***** Process Selected Layers from Jazz-y *****/
var s2t = stringIDToTypeID;
(r = new ActionReference).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var lrs = executeActionGet(r).getList(p),
sel = new ActionReference();
for (var i = 0; i < lrs.count; i++) {
sel.putIdentifier(s2t('layer'), p = lrs.getReference(i).getIdentifier(s2t('layerID')));
(r = new ActionReference).putIdentifier(s2t('layer'), p);
(d = new ActionDescriptor()).putReference(s2t("target"), r);
executeAction(s2t('select'), d, DialogModes.NO);
/***** Process Selected Layers from Jazz-y *****/
if (app.documents.length > 0) {
app.activeDocument.activeLayer.name = promptString;
} else {
alert("You must have a document open!");
}
}
}
app.activeDocument.suspendHistory("Rename selected layers", "main()");
But what do i need to add to make it add incremental numbers to each of the layers?
thanks
im struggling with this
ian
A different script, not my code. The incremental number is added as a suffix, you may wish to put a space, hyphen, underscore or another at the end of the name new layer name as the sequential number is added at the end. Works on selected layers, the start number can be changed which is nice. Renames from the bottom of the panel up by default:
#target photoshop
app.bringToFront();
main();
function main(){
if(!documents.length) return;
var selectedLayers = getSelectedLayersIdx();
var win = new Window( 'dialog', '' );
g = win.graphics;
var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.2, 0.2, 0.2, 1]);
g.backgroundColor = myBrush;
win.orientation='stack';
win.p1= win.add("panel", undefined, undefined, {borderStyle:"black"});
win.g1 = win.p1.add('group');
win.g1.orientation = "row";
win.title = win.g1.add('statictext',undefined,'Rename Layers');
win.title.alignment="fill";
var g = win.title.graphics;
g.font = ScriptUI.newFont("Georgia","BOLDITALIC",22);
win.g5 =win.p1.add('group');
win.g5.orientation = "column";
win.g5.alignChildren='left';
win.g5.spacing=10;
win.g5.st1 = win.g5.add('statictext',undefined,'New layer name');
win.g5.et1 = win.g5.add('edittext');
win.g5.et1.preferredSize=[250,20];
win.g10 =win.p1.add('group');
win.g10.orientation = "row";
win.g10.alignment='fill';
win.g10.spacing=10;
win.g10.st1 = win.g10.add('statictext',undefined,'Serial Number');
win.g10.et1 = win.g10.add('edittext',undefined,'1');
win.g10.et1.preferredSize=[50,20];
win.g10.et1.onChanging = function() {
if (this.text.match(/[^\-\.\d]/)) {
this.text = this.text.replace(/[^\-\.\d]/g, '');
}
};
win.g10.st1 = win.g10.add('statictext',undefined,'Length');
var nums=[2,3,4,5];
win.g10.dl1 = win.g10.add('dropdownlist',undefined,nums);
win.g10.dl1.selection=0;
win.g15 =win.p1.add('group');
win.g15.orientation = "row";
win.g15.alignment='fill';
win.g15.cb1 = win.g15.add('checkbox',undefined,'Reverse layer order');
win.g100 =win.p1.add('group');
win.g100.orientation = "row";
win.g100.alignment='center';
win.g100.spacing=10;
win.g100.bu1 = win.g100.add('button',undefined,'Rename');
win.g100.bu1.preferredSize=[120,30];
win.g100.bu2 = win.g100.add('button',undefined,'Cancel');
win.g100.bu2.preferredSize=[120,30];
win.g100.bu1.onClick=function(){
if(win.g5.et1.text == ''){
alert("No layer name has been entered!");
return;
}
win.close(0);
if(win.g15.cb1.value) selectedLayers.reverse();
for(var a in selectedLayers){
var LayerName = win.g5.et1.text + zeroPad((Number(win.g10.et1.text)+Number(a)), Number(win.g10.dl1.selection.text));
putLayerNameByIndex(Number(selectedLayers[a]), LayerName );
}
}
win.center();
win.show();
}
function getSelectedLayersIdx(){
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count
var selectedLayers = new Array();
for(var i=0;i<c;i++){
try{
activeDocument.backgroundLayer;
selectedLayers.push( desc.getReference( i ).getIndex() );
}catch(e){
selectedLayers.push( desc.getReference( i ).getIndex()+1 );
}
}
}else{
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
try{
activeDocument.backgroundLayer;
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
}catch(e){
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
}
var vis = app.activeDocument.activeLayer.visible;
if(vis == true) app.activeDocument.activeLayer.visible = false;
var desc9 = new ActionDescriptor();
var list9 = new ActionList();
var ref9 = new ActionReference();
ref9.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
list9.putReference( ref9 );
desc9.putList( charIDToTypeID('null'), list9 );
executeAction( charIDToTypeID('Shw '), desc9, DialogModes.NO );
if(app.activeDocument.activeLayer.visible == false) selectedLayers.shift();
app.activeDocument.activeLayer.visible = vis;
}
return selectedLayers;
};
function zeroPad(n, s) {
n = n.toString();
while (n.length < s) n = '0' + n;
return n;
};
function putLayerNameByIndex( idx, name ) {
if( idx == 0 ) return;
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( 'Lyr ' ), idx );
desc.putReference( charIDToTypeID('null'), ref );
var nameDesc = new ActionDescriptor();
nameDesc.putString( charIDToTypeID('Nm '), name );
desc.putObject( charIDToTypeID('T '), charIDToTypeID('Lyr '), nameDesc );
executeAction( charIDToTypeID( 'slct' ), desc, DialogModes.NO );
executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
}
The previous script renames all selected, variable-named layers to a common, static name + incremental number suffix.
For added flexibility, I have adapted this code to either prefix or suffix the incremental numbering without changing the original variable layer name.
It greatly speeds up the process of selecting and renaming layers. And can also perform other batch process that have always been a big pain when working with a large number of layers.
The free version is limited to 5 actions. The paid version is 12$.
Gotta say REALLY disappointed with Swift, there is no obvious warning that it has only 5 tries before it dies - so after trying to figure it out without killing my project, it suddenly points out that it is now dead and I need to PAY for it to continue.
On the adobe site it says FREE... I saw nothing saying 5 butons presses...
Now I'm completely cool with being asked to pay for it but it's a bit rum when you fiddle with it and then it stops and asks for money. Without a heads up.
Sorry to hear you are having a probem. It does say in the discription of the Trial version that its limited to 5 actions. But maybe it would be a good idea to have a dialong box at the start as well.
I think you are right. I'll make some changes to the trial next week.
I did not really make the plug-in for money I made it for my own company. We save a lot of time using it. Send us a mail and I can help worth the Version you have.
Try the various scripts in this topic, arboards/layers/groups should all be interchangeable unless a specific script is targeting a specific layer kind.
Sorry to reserruct this post, default behavior in windows file explorer, even back in 2012, was > Select Multiple Files > Rename The File > Files are then named iteratively; File(1) File(2) File(3).
This is the kind of functionality one would expect from a computer program that involves complex data management.