#Copying layers label colour with scripting

1 messages · Page 1 of 1 (latest)

humble abyss
#

Hey there, 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 :)

slim heron
#

Gave links to Photoshop scripts forums in the main forum

humble abyss
#

@boreal forge Thanks for the answer

fast gladeBOT
#

Gave +1 Creative Carma to @boreal forge (current: #14 - 227)

humble abyss
#

No wonder it feels alien compared to the JS code in that link

#

And yeah thats what I mean with label colour

#

Idk if there is a better name for it or not 😅

boreal forge
# humble abyss And yeah thats what I mean with label colour

AM Code is specific to Photoshop and works well with Adobe Extend Script (JavaScript), but it is much more difficult to learn and edit.

I still have a few code snippets for layer colours ‘lying around’ at home, but I'm not sure right now whether they are Illustrator or Photoshop scripts. I'll be home in a few hours. Then I'll take a look and get back to you.

humble abyss
boreal forge
# humble abyss Great, I appreciate taking your time :) As far as I remember we tried to build...

@humble abyss
I could not find it on my current computer. But this snippet that I wrote years ago in the Photoshop scripting forum should help you:
https://community.adobe.com/t5/photoshop-ecosystem-discussions/determine-layer-color-label-from-layer-id/m-p/8708077#M59276

humble abyss
# boreal forge <@187234178042036225> I could not find it on my current computer. But this snip...

Hey thank you. They are definitely working when I try them but I honestly can't even imagine how I can combine them with my script.

I mean from what I can see I should get the color after script determines which layer is the bottom one (since I pick more than one layer) but I am not even sure which part of the script does that.. I haven't seen many coding languages but this is definitely the most alien one.

May I ask your help if its not too much hassle?

fast gladeBOT
#

Gave +1 Creative Carma to @boreal forge (current: #14 - 229)

boreal forge
humble abyss
#

So, this.

humble abyss
humble abyss
boreal forge
humble abyss
#

And there is also the issue of first function doesn't get the color information if there are more than one layers

boreal forge
humble abyss
#

I was looking at the original script again and I am wondering it is possible to just alter a bit instead of creating 2 new functions?

Probably getting a new variable on 1, and somehow using it near 2?

boreal forge
humble abyss
boreal forge
# humble abyss Actually I would be ok with JS as well but honestly I couldn't understand what y...

That was your original enquiry:

… 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?

And this:

… But as you can see it the created smart object has no color. What I would want is simply having the color of Layer1 which is blue …

The solutions shown fulfil exactly these tasks (and even a little more).

It's time for me to go to bed now. I'm going to retire.
Good luck with the implementation.
🙂

humble abyss
fast gladeBOT
#

Gave +1 Creative Carma to @boreal forge (current: #14 - 231)

vagrant sky
#

In your case u can use Alchemist for Photoshop

Listens all (most of) Photoshop events. Something like actions panel in Photoshop but for scripting. Or ScriptListener plugin for ExtendScript but in panel. Also can inspect PS DOM and show PS AM descriptors from various places.

https://github.com/jardicc/alchemist

GitHub

DevTool for plugin developers. Contribute to jardicc/alchemist development by creating an account on GitHub.

#

grabs the selected layer IDs,
2. reads the name and label color from the bottom-most selected layer,
3. converts the selection to a Smart Object,
4. renames the SO,
5. applies the same label color to the SO.

#

Works with standard label colors: none, red, orange, yellow, green, blue, violet, gray.
• If the bottom layer has no label (or Photoshop can’t report it), the SO gets none.
• This keeps their original behavior (naming from the bottom layer) and just adds color copy.
• Tested logic relies on Action Manager (same approach as that StackOverflow thread). No DOM property exists for label color, so AM is the right way.

#
// PART 1 code here (from "#target photoshop" down to "newPlacedLayer")

#target photoshop
app.activeDocument.suspendHistory("Smart Obj Script (name + color)", "main()");

function main () {
    var s2t = stringIDToTypeID;

    // --- Get selected layers
    var ref = new ActionReference();
    ref.putProperty(s2t('property'), s2t('targetLayersIDs'));
    ref.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var idList = executeActionGet(ref).getList(s2t('targetLayersIDs'));
    if (!idList || idList.count === 0) return;

    // --- Bottom layer ID
    var bottomLayerID = idList.getReference(idList.count - 1).getIdentifier(s2t('layerID'));

    // --- Get bottom name
    var rName = new ActionReference();
    rName.putProperty(s2t('property'), s2t('name'));
    rName.putIdentifier(s2t('layer'), bottomLayerID);
    var bottomName = executeActionGet(rName).getString(s2t('name'));

    // --- Get bottom color
    var rColor = new ActionReference();
    rColor.putProperty(s2t('property'), s2t('color'));
    rColor.putIdentifier(s2t('layer'), bottomLayerID);
    var colorEnum;
    try {
        colorEnum = executeActionGet(rColor).getEnumerationValue(s2t('color'));
    } catch (e) {
        colorEnum = s2t('none');
    }

    // --- Convert to Smart Object
    executeAction(s2t('newPlacedLayer'), undefined, DialogModes.NO);

#
// PART 2 code here (from "// --- Rename SO" to the closing brace)

    // --- Rename SO
    var refSO = new ActionReference();
    refSO.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
    var dSetName = new ActionDescriptor();
    dSetName.putReference(s2t('null'), refSO);
    var dToName = new ActionDescriptor();
    dToName.putString(s2t('name'), bottomName);
    dSetName.putObject(s2t('to'), s2t('layer'), dToName);
    executeAction(s2t('set'), dSetName, DialogModes.NO);

    // --- Apply color
    var refSO2 = new ActionReference();
    refSO2.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
    var dSetColor = new ActionDescriptor();
    dSetColor.putReference(s2t('null'), refSO2);
    var dToColor = new ActionDescriptor();
    dToColor.putEnumerated(s2t('color'), s2t('color'), colorEnum);
    dSetColor.putObject(s2t('to'), s2t('layer'), dToColor);
    executeAction(s2t('set'), dSetColor, DialogModes.NO);
}

humble abyss