Skip to main content
Participating Frequently
March 22, 2012
Released

P: Rename multiple layers in one go

  • March 22, 2012
  • 22 replies
  • 20070 views

Renaming each individual layer is rather slow, what if we can rename all selected layers at once?

22 replies

e-onn
Inspiring
March 15, 2023

Thanks so much @Stephen Marsh  this works brilliant.

 

Stephen Marsh
Community Expert
Community Expert
March 14, 2023

@e-onn 

 

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.

 

Add Number Prefix to Selected Layers:

 

// Code removed due to error

 

 

Add Number Suffix to Selected Layers:

 

// Code removed due to error

 

Stephen Marsh
Community Expert
Community Expert
March 14, 2023
quote

Hi Stephen your reamer script is great.

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 );
}

 

 

This one renames all layers, not selected:

 

https://morris-photographics.com/photoshop/scripts/rename-layers.html

 

e-onn
Inspiring
March 14, 2023

Hi Stephen your reamer script is great.

But what do i need to add to make it add incremental numbers to each of the layers?

thanks

im struggling with this

 

ian

Participant
January 25, 2022

Thank you. Does this work with renaming artboards as well?

Stephen Marsh
Community Expert
Community Expert
January 25, 2022

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()");

 

 

 

Stephen Marsh
Community Expert
Community Expert
January 25, 2022
quote

I need to do this though. Is there a quick way to do it without having to copy and past over and over again?

 

@Julien5C96 

 

You can do this using a free script from Paul Riggott. The find is regular expression based, so .+ will find the entire layer name:

 

 

https://github.com/Paul-Riggott/PS-Scripts/blob/master/Layer%20Name%20Edit.jsx

 

Change line 20 from:

 

var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.99, 0.99, 0.99, 1]);

 

to something like:

 

var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.2, 0.2, 0.2, 1]);

 

For a "dark mode" interface.

 

EDIT: Or perhaps even better:

 

var myBrush = g.newBrush(g.BrushType.THEME_COLOR, "appDialogBackground");

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

Participant
January 24, 2022

I need to do this though. Is there a quick way to do it without having to copy and past over and over again?

Participating Frequently
March 22, 2012
Yes... I know what you mean though but there are instances where I've needed to name things the same.
Legend
March 22, 2012
Is that what you were looking for? I couldn't see wanting to name 20 layers all the same name. 😉