Reputation: 63
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;
};
Upvotes: 0
Views: 1217
Reputation: 3082
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.
Upvotes: 1