Skip to main content
Mugen777
Inspiring
September 29, 2025
Answered

How to copy/apply a layers label colour with scripting?

  • September 29, 2025
  • 1 reply
  • 375 views

Hey everyone, long ago I made this script with some help but right now I totally forget all about scripting and I need a little update.

 

What it does is just combining several layers into a smart object, and rename it to the bottom layer. Now, if possible I want to copy the layer's color as well. Can someone help me achieve this?

 

This is the script

 

function main() {
var s2t = stringIDToTypeID;

(tr = new ActionReference).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
tr.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));

(lr = new ActionReference).putProperty(s2t('property'), n = s2t('name'));
lr.putIdentifier(s2t('layer'), executeActionGet(tr).getList(p).getReference(0).getIdentifier(s2t('layerID')));
var nm = executeActionGet(lr).getString(n)

executeAction(s2t('newPlacedLayer'), undefined, DialogModes.NO);

(r = new ActionReference()).putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
(d = new ActionDescriptor()).putReference(s2t("null"), r);
(d1 = new ActionDescriptor()).putString(s2t("name"), nm);
d.putObject(s2t("to"), s2t("layer"), d1);
executeAction(s2t("set"), d, DialogModes.NO);
}

app.activeDocument.suspendHistory("Smart Obj Script", "main()");

 

I also found this post which should help I think but I am unable to apply it on my script.

 

https://stackoverflow.com/questions/40972194/how-to-programmatically-access-layer-tag-colour-in-photoshop

 

Help really appreciated, thanks 🙂

Correct answer Stephen Marsh

Btw just to be clear the main point of the script was that it uses name of the bottom layer (while right click > turn to smart object uses name of the top layer) of selected layers. Its pretty important for me to make it work that way. 


@Mugen777 

 

Understood, the code that I posted was using the active layer, not the layer ID of the bottom layer. So it requires modification.

 

Here is the code with comments and renamed variables to make it clearer what is taking place:

#target photoshop

// Run the script inside a single history state
app.activeDocument.suspendHistory("Smart Obj Script", "main()");

function main() {

    var s2t = stringIDToTypeID;

    // Get the ID of the currently selected layer/s
    (theLayerIDs = new ActionReference).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    theLayerIDs.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));

    // targetLayersIDs returns a list ordered top-to-bottom in the Layers panel
    // .getReference(0) returns the first item in the list (i.e. the back/bottom layer)
    var theBackLayer = executeActionGet(theLayerIDs).getList(p).getReference(0).getIdentifier(s2t('theBackLayer'));

    // From theBackLayer get the layer name
    (theLayer = new ActionReference).putProperty(s2t('property'), theLayerName = s2t('name'));
    theLayer.putIdentifier(s2t('layer'), theBackLayer);
    var theName = executeActionGet(theLayer).getString(theLayerName);

    // From theBackLayer get the label colour
    (theLabelCol = new ActionReference).putProperty(s2t('property'), theCol = s2t('color'));
    theLabelCol.putIdentifier(s2t('layer'), theBackLayer);
    var col = typeIDToStringID(executeActionGet(theLabelCol).getEnumerationValue(theCol));

    // Convert the selected layer/s into a Smart Object
    executeAction(s2t('newPlacedLayer'), undefined, DialogModes.NO);

    // Create a reference to the new Smart Object layer
    (theSOlayer = new ActionReference()).putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));

    // Create an ActionDescriptor to rename the new Smart Object layer
    (d = new ActionDescriptor()).putReference(s2t("null"), theSOlayer);
    (d1 = new ActionDescriptor()).putString(s2t("name"), theName);
    d.putObject(s2t("to"), s2t("layer"), d1);
    executeAction(s2t("set"), d, DialogModes.NO);

    // Apply the original label colour to the new Smart Object
    setLayerLabelCol(col);
}

function setLayerLabelCol(labelCol) {
    var c2t = function (s) { return app.charIDToTypeID(s); };
    var s2t = function (s) { return app.stringIDToTypeID(s); };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    var reference = new ActionReference();
    reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
    descriptor.putReference(c2t("null"), reference);
    descriptor2.putEnumerated(s2t("color"), s2t("color"), s2t(labelCol));
    descriptor.putObject(s2t("to"), s2t("layer"), descriptor2);
    executeAction(s2t("set"), descriptor, DialogModes.NO);
}

 

1 reply

Stephen Marsh
Community Expert
Community Expert
September 29, 2025

@Mugen777 

 

To copy the active layer's colour label to a variable:

// Get the label color
var activeLayerColor = getLayerColour();

function getLayerColour() {
    // Courtesy of Paul Riggott
    // Colours returned:
    // "none","red","orange","yellowColor","grain","blue","violet","gray" - new in 2024: "magenta", "seafoam", "indigo", "fuchsia"
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var appDesc = executeActionGet(ref);
    return typeIDToStringID(appDesc.getEnumerationValue(stringIDToTypeID('color')));
}

 

To apply the colour label to another active layer:

 

// Set the label color
setLayerLabelCol(activeLayerColor);

function setLayerLabelCol(labelCol) {
    var c2t = function (s) {
        return app.charIDToTypeID(s);
    };
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    var reference = new ActionReference();
    reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
    descriptor.putReference(c2t("null"), reference);
    descriptor2.putEnumerated(s2t("color"), s2t("color"), s2t(labelCol));
    descriptor.putObject(s2t("to"), s2t("layer"), descriptor2);
    executeAction(s2t("set"), descriptor, DialogModes.NO);
}

 

For testing purposes:

 

try {
    var raw = getLayerColour();
    var names = {
        "none": "None",
        "red": "Red",
        "orange": "Orange",
        "yellowColor": "Yellow",
        "grain": "Green",
        "blue": "Blue",
        "violet": "Violet",
        "gray": "Gray",
        "magenta": "Magenta",
        "seafoam": "Seafoam",
        "indigo": "Indigo",
        "fuchsia": "Fuchsia"
    };
    var colorLabelMapping = names[raw] || raw || "Unknown";
    alert("Layer label color: " + colorLabelMapping); // mapped name
    // alert(raw); // unmapped name

} catch (e) {
    alert("Could not get layer color - make sure a layer is selected.\n\nError: " + e.message);
}

function getLayerColour() {
    // Courtesy of Paul Riggott
    // Colours returned (examples): 
    // "none","red","orange","yellowColor","grain","blue","violet","gray"
    // new in 2024: "magenta", "seafoam", "indigo", "fuchsia"
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var appDesc = executeActionGet(ref);
    return typeIDToStringID(appDesc.getEnumerationValue(stringIDToTypeID('color')));
}
Mugen777
Mugen777Author
Inspiring
September 29, 2025

Hey, thank you. May I ask merging it with the script if it's not too much of a hassle? 

 

Because it's been years since I tinker with any kind of coding and "my" script looks like an alien text, which was created with help back then anyway -_-

Stephen Marsh
Community Expert
Community Expert
September 29, 2025
quote

Hey, thank you. May I ask merging it with the script if it's not too much of a hassle? 


By @Mugen777


Please post a screenshot of the layers panel before the script is run (including selected layers).

 

Your script only converts the active layer to a smart object, or multiple selected layers. If you only have a single layer, then it's a no brainer to get the colour label. But what if there are multiple layers selected?