Introduction#
Renaming layers one by one is a very tedious and time-consuming task when dealing with .psd
files that have many layers in Photoshop.
After searching online, I found that major websites either require the installation of corresponding version plugins or the script release dates are very old, and they rename all layers violently. It is astonishing that Photoshop has developed for so many years without adding such a simple and efficient feature.
Below is a widely circulated version for reference.
app.bringToFront();
var YourName = prompt("Please enter the name you want to rename the layers to", 'Enter here')
if (documents.length == 0) {
alert("No layers");
}
else {
var visibility = false;
var docRef = activeDocument;
changeLayerName(docRef);
}
function changeLayerName(layer){
var layers = layer.layers;
if(layers){
for(var i = 0; i < layers.length; i ++){
layers[i].name = YourName + [i];
changeLayerName(layers[i]);
}
}
}
What I Need#
- Only modify the currently selected layers
- Layers support any hierarchy
- Can add numbered suffixes to these layers
- Ideally, it should also modify the
color tag
color label of the layers
Production Steps#
Create a new text file using a text editor, paste the code below, and you can achieve batch renaming of layers through the script feature of Adobe Photoshop 2023. This script can traverse all selected layers in the current document and prompt the user to enter a custom layer name and numbering format, then name each layer according to the user-specified format. The script will automatically handle whether the layers are in a layer group.
#target photoshop
app.bringToFront();
if (app.documents.length === 0) {
alert("No documents open.");
throw new Error("No document open.");
}
var doc = app.activeDocument;
var selectedLayers = getSelectedLayers();
if (selectedLayers.length === 0) {
alert("No layers selected.");
throw new Error("No layers selected.");
}
var baseName = prompt("Please enter the base layer name:", "Layer");
var startNumber = parseInt(prompt("Please enter the starting value for numbering:", "1"), 10);
var numberFormat = prompt("Please enter the numbering format (e.g., 001, keep the number of digits):", "001");
if (isNaN(startNumber) || !baseName || !numberFormat) {
alert("Invalid input, please run the script again.");
throw new Error("Invalid input.");
}
renameLayers(selectedLayers, baseName, startNumber, numberFormat);
function getSelectedLayers() {
var selectedLayers = [];
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var desc = executeActionGet(ref);
if (desc.hasKey(stringIDToTypeID('targetLayers'))) {
var targetLayers = desc.getList(stringIDToTypeID('targetLayers'));
for (var i = 0; i < targetLayers.count; i++) {
var layerIndex = targetLayers.getReference(i).getIndex();
selectedLayers.push(getLayerByIndex(layerIndex + 1));
}
} else {
selectedLayers.push(doc.activeLayer);
}
return selectedLayers;
}
function getLayerByIndex(index) {
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var desc = executeActionGet(ref);
var layerID = desc.getInteger(stringIDToTypeID("layerID"));
return getLayerById(layerID);
}
function getLayerById(id) {
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), id);
var desc = executeActionGet(ref);
return doc.layers.getByName(desc.getString(charIDToTypeID("Nm ")));
}
function renameLayers(layers, baseName, startNumber, numberFormat) {
for (var i = 0; i < layers.length; i++) {
var currentNumber = (startNumber + i).toString();
var formattedNumber = zeroPad(currentNumber, numberFormat.length);
var newName = baseName + formattedNumber;
layers[i].name = newName;
}
}
function zeroPad(num, width) {
while (num.length < width) {
num = '0' + num;
}
return num;
}
The above code implements basic layer renaming and numbering functionality. Save the file as xxx.jsx
.
Usage Steps:#
- Open Adobe Photoshop 2023.
- Select the layers you want to rename (you can select multiple layers in the layers panel, including layers nested in layer groups).
- Go to
File
->Scripts
->Browse...
, and select the script file you saved. - After executing the script, three prompt boxes will appear:
- The first prompt box asks for the base layer name, for example, "Layer".
- The second prompt box asks for the starting value for numbering, for example, "1".
- The third prompt box asks for the numbering format, for example, "001", ensuring the numbering maintains a consistent number of digits.
- After confirming, the script will automatically rename the selected layers according to the format you set.
Advanced Version#
The above functionality is generally sufficient, but if you need to add the ability to modify color tags simultaneously, you can download the advanced version. The effect is as follows:
Download link:
【LayerRenamer_PS-2023.jsx】 https://16b87ca7d6.znas.cn/AppH5/share/?nid=LIYDEMJQGBBDEOCELBIFU&code=r1DtQDtFZobo1ai8Jd0UylatvnNkQ3xcodyiJBPo4ejLcOfybeyVGW0o3LOTKTHF&mode=file&display=list Valid for 7 days, extraction password: 6633
This article was first published on Minority.