Copy link to clipboard
Copied
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.
Help really appreciated, thanks 🙂
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
...
Copy link to clipboard
Copied
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')));
}
Copy link to clipboard
Copied
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 -_-
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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?
For example this is how the layers look initially
This is how I want it to look after using the script
This is how it looks right now
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
@Mugen777 - You're welcome!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now