Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Contributor ,
Sep 29, 2025 Sep 29, 2025

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-phot...

 

Help really appreciated, thanks 🙂

TOPICS
Actions and scripting
268
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 29, 2025 Sep 29, 2025

@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 A
...
Translate
Adobe
Community Expert ,
Sep 29, 2025 Sep 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')));
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 29, 2025 Sep 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 -_-

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2025 Sep 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 29, 2025 Sep 29, 2025

While I am not %100 sure how it works, I think before turning layers into smart object, script gets the name of the bottom selected layer, then turns them into smart object and then applies the name. So in theory just getting the color as well should work? 

 

1000008365.jpg

For example this is how the layers look initially

 

1000008364.jpg

This is how I want it to look after using the script

 

1000008366.jpg

This is how it looks right now

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 29, 2025 Sep 29, 2025

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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2025 Sep 29, 2025

@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);
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 30, 2025 Sep 30, 2025

Its just perfect. This time language is something I can somewhat understand as well, especially with comments. Maybe I can change something in the future if I need to this way 🙂 

 

Thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 07, 2025 Oct 07, 2025
LATEST

@Mugen777 - You're welcome!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines