Skip to main content
Known Participant
January 10, 2023
Answered

PS Script – Add a prefix to the names of the selected layers

  • January 10, 2023
  • 1 reply
  • 4520 views

Hi everyone! 

I'm looking for a script that will add certain text to their names to selected layers. Example:

1. I manually select the layers in which I want to rename.
2. I run the script.
3. A window opens where I enter the text, e.g. Bottle.
4. The text is added at the beginning of the name of the selected files (prefix).

Do you know something like that, or if it's not too difficult, could you help me create it?

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

Sorry, my mistake, the parent group wasn't selected! :]

 

Try this, it doesn't rename a selected Background image layer, but it could. It uses a space separator between the entered prefix and the existing layer name. A single undo history step is provided:

 

#target photoshop

function main() {

    (function () {

        var prefix = prompt("Enter the layer name prefix:", "");
                if (prefix === null) {
            return;
        }

        // Get the selected layers: courtesy of jazz-y
        var s2t = stringIDToTypeID;
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
        r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
        var lrs = executeActionGet(r).getList(p),
            sel = new ActionReference();

        // Loop over the selected layers: courtesy of jazz-y
        for (var i = 0; i < lrs.count; i++) {
            sel.putIdentifier(s2t('layer'), p = lrs.getReference(i).getIdentifier(s2t('layerID')));
            (r = new ActionReference()).putIdentifier(s2t('layer'), p);
            (d = new ActionDescriptor()).putReference(s2t("target"), r);
            executeAction(s2t('select'), d, DialogModes.NO);

            var layerName = activeDocument.activeLayer.name;

            // Rename the selected layers
            if (activeDocument.activeLayer.isBackgroundLayer === false) {
                activeDocument.activeLayer.name = prefix + " " + layerName;
            }
        }
   
    })();
    
}

// Single history stage undo
activeDocument.suspendHistory("Undo script...", "main()");

 

1 reply

Stephen Marsh
Community Expert
Community Expert
January 10, 2023

Do you wish this for selected layers... Or for all top-level or nested layers contained within a layer group?

 

I ask as you mention selecting the layers, however, the screenshot possibly shows something different (the parent layer group was selected but not renamed with a prefix).

KHKHAuthor
Known Participant
January 10, 2023

I only need for selected layers. Sorry maybe it's illegible on the screen I attached, but there are 4 layers selected there: GOLD, HIGHLIGHTS, SHADOWS and COLOR 🙂

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
January 10, 2023

Sorry, my mistake, the parent group wasn't selected! :]

 

Try this, it doesn't rename a selected Background image layer, but it could. It uses a space separator between the entered prefix and the existing layer name. A single undo history step is provided:

 

#target photoshop

function main() {

    (function () {

        var prefix = prompt("Enter the layer name prefix:", "");
                if (prefix === null) {
            return;
        }

        // Get the selected layers: courtesy of jazz-y
        var s2t = stringIDToTypeID;
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
        r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
        var lrs = executeActionGet(r).getList(p),
            sel = new ActionReference();

        // Loop over the selected layers: courtesy of jazz-y
        for (var i = 0; i < lrs.count; i++) {
            sel.putIdentifier(s2t('layer'), p = lrs.getReference(i).getIdentifier(s2t('layerID')));
            (r = new ActionReference()).putIdentifier(s2t('layer'), p);
            (d = new ActionDescriptor()).putReference(s2t("target"), r);
            executeAction(s2t('select'), d, DialogModes.NO);

            var layerName = activeDocument.activeLayer.name;

            // Rename the selected layers
            if (activeDocument.activeLayer.isBackgroundLayer === false) {
                activeDocument.activeLayer.name = prefix + " " + layerName;
            }
        }
   
    })();
    
}

// Single history stage undo
activeDocument.suspendHistory("Undo script...", "main()");