Copy link to clipboard
Copied
Hi all,
Below is a snippet of code used to sort through a collection of layers and place them into a similarly named group. Works great. Is there a way for the code to be augmented to include recognizing a smart object layer? The code below only seems to work with pixel layers.
Copy link to clipboard
Copied
I'm not in front of a computer for testing, but try:
var layers = app.activeDocument.layers;
That being said, I'd avoid declaring the variable name as layers, but see how this goes.
Copy link to clipboard
Copied
Yeah, that's not working. The script sees the file name but has a conflict at the group name stage. Error 1220:Illegal Argument. I guess that's why the distinction was made between the app.activeDocument.artLayers and pGroup=activeDocument.layers
Copy link to clipboard
Copied
Layer groups should be specifically identified as layerSet (singular) or layerSets (collective).
A regular pixel layer and a smart object layer both have the layer type of ArtLayer.
I'll come back to you later...
Copy link to clipboard
Copied
Thanks for that insight. Based on your comment about the artLayers defining either regular or SO layers, I went to a previously approved file. Input all as Smart Object layers and the script saw and moved them to their correct groups.
So in a nutshell, it's good.
BTW, do you, did you fequent Dan Margulius' color Theory group?
Copy link to clipboard
Copied
Thanks for that insight. Based on your comment about the artLayers defining either regular or SO layers, I went to a previously approved file. Input all as Smart Object layers and the script saw and moved them to their correct groups.
So in a nutshell, it's good.
That's great, I'll stop banging my head against the wall then!
BTW, do you, did you fequent Dan Margulius' color Theory group?
By @RoyBoySolorio
Yes, one and the same, I was very active there, however, since the move to groups.io I have been locked out of 2FA so I can't post via the web anymore, rarely I try an email to see if makes it through.
Copy link to clipboard
Copied
Start the banging again Steven.
I have 19 locales that when sorted, as the snipet of script above shows, places the layer into it's layer group i.e. layer name "app_enUS_Classic_Car" gets sorted into the group "enUS" The script sees the enUS in the layer name and moves it into the group of the same name. Cool. And if there are 19 layers it works flawlessly.
What's not cool is that if there are only 3 locales. it will randomly place the locales in random groups, ignoring the name. Is there more code that can be added to ignore the missing layers but target more accurately the ones that are?
Copy link to clipboard
Copied
I just realised that we didn't specifically discuss your original question regarding how to recognise a smart object layer from other layers.
/*
Standard DOM Code
*/
if (activeDocument.activeLayer.kind == LayerKind.SMARTOBJECT) {
// More code here
}
or
/*
AM Code
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-check-layer-kind-using-javascript/m-p/13174707
*/
s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerKind'));
r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
var layerKind = executeActionGet(r).getInteger(p);
if (layerKind == 5) {
// More code here
}
On your new question, what if you put the code to move the selected layer into the layerSet into the for loop, rather than having it outside?
//// find enUS UI
var layers = app.activeDocument.artLayers;
for (var i = 0; i < layers.length; i++) {
if (/enUS/.test(layers[i].name)) { // layers[i] name contains "UI name";
try {
(ref1 = new ActionReference()).putName(stringIDToTypeID('layer'), layers[i].name);
(desc1 = new ActionDescriptor()).putReference(stringIDToTypeID('null'), ref1);
executeAction(stringIDToTypeID('select'), desc1, DialogModes.NO);
//Move enUS layer into layerset
var pGroup = activeDocument.layers.getByName('enUS'); // or whatever the layerset name is
activeDocument.activeLayer.move(pGroup, ElementPlacement.INSIDE); // move the just copied layer into the target group
} catch (e) {
alert("Sorry, this layername does not exists");
}
}
}
I'm guessing that it makes no difference as you only posted a snippet and it may be different in the full code... It may help to share full code and screenshots of the layers panel that works or fails and or sample files for testing.
Copy link to clipboard
Copied
Thanks Stephen, I plopped the code in, changing the names to ident the layers, but it only highlighted them then gave the built in alert.
"Sorry, this layername does not exists");
The tried the next named layer, alert, etc.
I could try and put something together...next week. The code I have works for the full suite of names, it just breaks when there is less of them. Better said, it sorts some and others not so much.
Anyway have a great weekend.
Copy link to clipboard
Copied
Hi all,
Steven you asked for files that you can play with. Included in the link are a Mockup psd. 2 Actions and THE script in it's present state. Like I said, it works flawlessly when all 19 locales are available.
Link;
http://ftp.wearexyz.com/_IwYExdtNwtlh1R
Open the Smart Object and run the action "Build out 19 local group and LCs"
Then the action "Sort UIs into Named Folders" (The action calls on THE script so that will need to be resolved)
Notice how most of the UIs go into their correct folders, but others don't. That's what I'm trying to solve.
Copy link to clipboard
Copied
@RoyBoySolorio – Thank you for the helpful, detailed instructions. I can reproduce the issue... Now to try to find out why this is happening! :]
Copy link to clipboard
Copied
I believe that I have an answer!
The issue appears to be the case-sensitive test condition. Changing all occurrences of this to a case-insensitive test fixes the issue.
So, search and change all occurrences of:
/.test
To:
/i.test
For full context, from:
{ if (/enUS/.test(layers[i].name)) { // layers[i] name contains "UI name";
To:
{ if (/enUS/i.test(layers[i].name)) { // layers[i] name contains "UI name";
Copy link to clipboard
Copied
As an alternative to your script, you might find this one simpler (or at least shorter, not that this means anything):
var theLayers = activeDocument.artLayers;
for (var i = 0; i < theLayers.length; i++) {
while (theLayers.length > 1) {
var theLayer = theLayers[i];
var layerName = theLayer.name;
if (layerName.indexOf(layerName) === 0) {
var setName = layerName.toString().replace(/^image_/, '');
var theSet = activeDocument.layerSets.getByName(setName);
theLayer.move(theSet, ElementPlacement.INSIDE);
}
}
}
Copy link to clipboard
Copied
Your new code worked great, until I used a real set with names like "app_enUS_20_App_Notification" then it failed. I'll keep at it, but I appreciate the work Stephen.
Copy link to clipboard
Copied
Oh and the modified first code still through out files into random folders.