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

recognizing Smart Objects in layer sorting script.

Participant ,
Feb 13, 2023 Feb 13, 2023

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.

 

//// 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);
} catch (e) { alert("Sorry, this layername does not exists"); } } }

//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
~Roy
TOPICS
Actions and scripting

Views

3.5K

Translate

Translate

Report

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
Adobe
Community Expert ,
Feb 13, 2023 Feb 13, 2023

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.

Votes

Translate

Translate

Report

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
Participant ,
Feb 13, 2023 Feb 13, 2023

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

 

~Roy

Votes

Translate

Translate

Report

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 ,
Feb 13, 2023 Feb 13, 2023

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

Votes

Translate

Translate

Report

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
Participant ,
Feb 13, 2023 Feb 13, 2023

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?

~Roy

Votes

Translate

Translate

Report

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 ,
Feb 13, 2023 Feb 13, 2023

Copy link to clipboard

Copied

quote

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!

 

quote

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.

 

Votes

Translate

Translate

Report

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
Participant ,
Feb 24, 2023 Feb 24, 2023

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?

~Roy

Votes

Translate

Translate

Report

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 ,
Feb 24, 2023 Feb 24, 2023

Copy link to clipboard

Copied

@RoyBoySolorio 

 

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.

Votes

Translate

Translate

Report

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
Participant ,
Feb 25, 2023 Feb 25, 2023

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.

 

 

~Roy

Votes

Translate

Translate

Report

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
Participant ,
Feb 27, 2023 Feb 27, 2023

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.  

~Roy

Votes

Translate

Translate

Report

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 ,
Feb 28, 2023 Feb 28, 2023

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! :]

Votes

Translate

Translate

Report

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 ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

@RoyBoySolorio 

 

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";

Votes

Translate

Translate

Report

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 ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

@RoyBoySolorio 

 

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

 

 

 

Votes

Translate

Translate

Report

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
Participant ,
Mar 01, 2023 Mar 01, 2023

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.

~Roy

Votes

Translate

Translate

Report

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
Participant ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

LATEST

Oh and the modified first code still through out files into random folders.

~Roy

Votes

Translate

Translate

Report

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