Skip to main content
Known Participant
January 16, 2024
Answered

Rename all layers except Background Layer

  • January 16, 2024
  • 2 replies
  • 1226 views

I have imported 3 images in a PS document and I want to change name of all layers (except the bottom background layer) to say "Parrot"

So this can be like:

Parrot

Parrot

Parrot

 

OR

 

Parrot-01

Parrot-02

Parrot-03

 

3 images are for reference so this can be any N number of images.

Can someone help please?

This topic has been closed for replies.
Correct answer Stephen Marsh

No, that is all required.

If skipping works, it is fine.

 

The bottom layer is the one which needs not to be changed.

It is already named as Background and needs to remain with the same name.


The following adds the digit for 3 or more layers, but no zero padding:

 

#target photoshop

function main() {
    for (var i = 0; i < activeDocument.layers.length; i++) {
        if (activeDocument.layers.length == 2) {
            activeDocument.layers[0].name = "Parrot";
        } else if (activeDocument.layers.length > 2 && activeDocument.layers[i].name != "Background" && activeDocument.layers[i] != activeDocument.layers[app.activeDocument.layers.length - 1]) {
            activeDocument.layers[i].name = "Parrot-" + (i + 1);
        }
    }
}

activeDocument.suspendHistory("Undo Rename Layers Script...", "main()");

 

This version adds zero padding:

 

#target photoshop

function main() {

    var zeroPadLength = 2;
    var startNumber = 1;

    for (var i = 0; i < activeDocument.layers.length; i++) {
        if (activeDocument.layers.length == 2) {
            activeDocument.layers[0].name = "Parrot";
        } else if (activeDocument.layers.length > 2 && activeDocument.layers[i].name != "Background" && activeDocument.layers[i] != activeDocument.layers[app.activeDocument.layers.length - 1]) {
            activeDocument.layers[i].name = "Parrot-" + zeroPad(startNumber, zeroPadLength);
            startNumber++
        }

        function zeroPad(num, digit) {
            var tmp = num.toString();
            while (tmp.length < digit) {
                tmp = "0" + tmp;
            }
            return tmp;
        }
    }
}

activeDocument.suspendHistory("Undo Rename Layers Script...", "main()");

 

2 replies

PedroMacro
Participating Frequently
January 16, 2024

This script will work fine, it renames the bottom layer to background, then rename all other layers in a decrescent order from top to bottom.

// Function to rename all layers
function renameLayers() {
  var doc = app.activeDocument;
  var layers = doc.layers;
  var totalLayers = layers.length;
  
  // Rename the bottom layer to "Background"
  var bottomLayer = layers[totalLayers - 1];
  bottomLayer.name = "Background";
  
  // Rename the remaining layers
  for (var i = 0; i < totalLayers - 1; i++) {
    var layer = layers[i];
    var newName = "Parrot " + (i + 1);
    layer.name = newName;
  }
}

// Execute the function
app.activeDocument.suspendHistory("Rename Layers", "renameLayers();");
PedroMacro
Participating Frequently
January 16, 2024

I meant to say ascending order.

Stephen Marsh
Community Expert
Community Expert
January 16, 2024

@c.pfaffenbichler – good find! That's a very versatile script!

 

@StarAG​ – It's always a good idea to have unique layer names.

 

This script will conditionally skip over a true Background layer (which is a special type of layer, not just a layer named Background):

 

#target photoshop
for (var i = 0; i < activeDocument.layers.length; i++) {
    if (activeDocument.layers[i].isBackgroundLayer === false) {
        activeDocument.layers[i].name = "Parrot-" + (i + 1);
    }
}

 

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

Stephen Marsh
Community Expert
Community Expert
January 17, 2024

@StarAG​ 

 

Sorry, I missed that you had zero padding in your example, here is a new script:

 

#target photoshop

var zeroPadLength = 2;
var startNumber = 1;

for (var i = 0; i < activeDocument.layers.length; i++) {
    if (!activeDocument.layers[i].isBackgroundLayer) {
        activeDocument.layers[i].name = "Parrot-" + zeroPad(startNumber, zeroPadLength);
        startNumber++
    }

    function zeroPad(num, digit) {
        var tmp = num.toString();
        while (tmp.length < digit) {
            tmp = "0" + tmp;
        }
        return tmp;
    }

}