简体   繁体   中英

Javascript to change first layer name in photoshop without knowing the name

Didn't quite find what I was looking for in search so asking for help. I have a Javascript that will loop in a group of images and stack them in Photoshop. Want to rename the layers as they are brought in. The first layer name always seems to be called “Layer 0” so I was able to select and change it using the following:

var doc = app.activeDocument;
doc.activeLayer = doc.artLayers.getByName("Layer 0");
// Execute Action
    try {
              var currentName = "Layer 0";
                    var newName = "Base_color";
          var currentLayer = doc.activeLayer;
          if (currentLayer.name == currentName) {
                    currentLayer.name = newName;
          };
catch (e) {
      alert('error’);
    }

Some of my fellow co-workers can't get script to run because their first layer name is not "Layer 0" but “RGBA” (which makes the script crash). Instead of doing an OR statement with “RGBA”, I would like to create code to change the name of the first layer regardless of whatever it's named to help keep it error proof.

Trying something like this:

var doc = app.activeDocument;
var firstlayername = app.activeDocument.activeLayer.name;
doc.activeLayer = firstlayername;
// Execute Action
    try {
          var currentName = firstlayername;
          var newName = "Base_color";
          var currentLayer = doc.activeLayer;
          if (currentLayer.name == currentName) {
                    currentLayer.name = newName;
          };
catch (e) {
      alert('error’);
    }

Cant get it to work. Is there a way to grab first layer name brought into Photoshop without knowing the name and change it?

Thanks!

Here is my loop:

// Stack images in order of numerical import. Remove all ending numbers.
while(app.documents.length>1){  
app.activeDocument = app.documents[1];  
var layerName = decodeURI(activeDocument.name).replace(/[0-9]*\....$/,'');  
activeDocument.activeLayer.duplicate(documents[0]);  
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);  
activeDocument.activeLayer.name = layerName;  
}; 

To rename the bottom layer:

var layers = app.activeDocument.artLayers;
var layer = layers[layers.length - 1];
layer.name = "Test name";

Note that this is not necessarily the first layer in order that the layers have been brought in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM