banner
CG艺术实验室

CG艺术实验室

数字艺术 / 视觉设计 / 技术分享 / 知识管理
tg_channel
facebook
x
github
bilibili
zhihu
email

LayerRenamer 1.0 script release, renaming PS layers has never been this easy

LayerRenamer 1.0 Script Release, PS Layer Renaming Has Never Been So Easy - Image 1

Introduction#

In graphic design, renaming layers one by one when handling .psd files with many layers in Photoshop is a very tedious and time-consuming task.

After searching online, I found that most major websites either require installing corresponding versions of plugins or have scripts that were released a long time ago, which violently rename all layers at once. It is quite 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", 'Please enter')
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]);
    }
}
}

The features I need are#

  • 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 scripting 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 number format (e.g., 001, keep the number of digits):", "001");

if (isNaN(startNumber) || !baseName || !numberFormat) {
    alert("Invalid input, please rerun the script.");
    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 sorting functions. Save the file as a script file named xxx.jsx.

Usage Steps:#

  1. Open Adobe Photoshop 2023.
  2. Select the layers you want to rename (you can select multiple layers in the layers panel, including layers nested in layer groups).
  3. Go to File -> Scripts -> Browse..., and select the script file you saved.
  4. 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 number format, for example, "001", ensuring that the numbering maintains a consistent number of digits.
  • LayerRenamer 1.0 Script Release, PS Layer Renaming Has Never Been So Easy - Image 2
  1. After confirming your selections, the script will automatically rename the selected layers according to the format you set.

Advanced Version#

The above features are basically sufficient, but if you need to add the ability to modify color tags simultaneously, you can download the advanced version. The effect is as shown below:

LayerRenamer 1.0 Script Release, PS Layer Renaming Has Never Been So Easy - Image 3

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.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.