Skip to main content
Simonetos The Greek
Inspiring
September 29, 2019
Question

How can I get layer's data from a specific group (layerSet) using Action Manager?

  • September 29, 2019
  • 7 replies
  • 1196 views

With this function, I get data of every layer with Bevel and Emboss effect within a document. I am looking for a way to do the same but within a specific group (layerSet). Thank you for your time!!!

 

function getLayersData()
{
    var lyrs = [];
    var layers = 1;
    while (true)
    {
        ref = new ActionReference();
        ref.putIndex(charIDToTypeID('Lyr '), layers);
        try
        {
            var d1 = executeActionGet(ref);
            var d2 = executeActionGet(ref);
            var d3 = executeActionGet(ref);
            var d4 = executeActionGet(ref);
        }
            catch (err)
        {
            break;
        };
        var c2t = function (s){return app.charIDToTypeID(s);};
        var s2t = function (s){return app.stringIDToTypeID(s);};
        var lyr = {};
        lyr.type = d1.getInteger(s2t("layerKind"));
        lyr.name = d1.getString(c2t("Nm  "));
        lyr.id = d1.getInteger(s2t("layerID"));
        if (checkbox1.value == true)
        {
            ref.putEnumerated( c2t("Lyr "), c2t("Ordn"), c2t("Trgt") );
            if (d1.hasKey(s2t('layerEffects')))
            {
                d1 = d1.getObjectValue(s2t('layerEffects'));
                lyr.scale = d1.getUnitDoubleValue(s2t('scale'));
                if (d1.hasKey(s2t('bevelEmboss')))
                {
                    d1 = d1.getObjectValue(s2t('bevelEmboss'));
                        lyr.enabled = d1.getBoolean(s2t('enabled'));
                        lyr.present = d1.getBoolean(s2t('present'));
                        lyr.showInDialog = d1.getBoolean(s2t('showInDialog'));
                        lyr.highlightMode = typeIDToStringID(d1.getEnumerationValue(s2t('highlightMode')));
                    d2 = d1.getObjectValue(s2t('highlightColor'));
                        lyr.red_hlc = d2.getDouble(s2t('red'));
                        lyr.grn_hlc = d2.getDouble(s2t('green'));
                        lyr.blue_hlc = d2.getDouble(s2t('blue'));
                        lyr.highlightOpacity = d1.getUnitDoubleValue(s2t('highlightOpacity'));
                        lyr.shadowMode = typeIDToStringID(d1.getEnumerationValue(s2t('shadowMode')))
                    d3 = d1.getObjectValue(s2t('shadowColor'));
                        lyr.red_sdc = d3.getDouble(s2t('red'));
                        lyr.grn_sdc = d3.getDouble(s2t('green'));
                        lyr.blue_sdc = d3.getDouble(s2t('blue'));
                        lyr.shadowOpacity = d1.getUnitDoubleValue(s2t('shadowOpacity'));
                        lyr.bevelTechnique = typeIDToStringID(d1.getEnumerationValue(s2t('bevelTechnique')));
                        lyr.bevelStyle = typeIDToStringID(d1.getEnumerationValue(s2t('bevelStyle')));
                        lyr.useGlobalAngle = d1.getBoolean(s2t('useGlobalAngle'));
                        lyr.localLightingAngle = d1.getUnitDoubleValue(s2t('localLightingAngle'));
                        lyr.localLightingAltitude = d1.getUnitDoubleValue(s2t('localLightingAltitude'));
                        lyr.strengthRatio = d1.getUnitDoubleValue(s2t('strengthRatio'));
                        lyr.blur = d1.getUnitDoubleValue(s2t('blur'));
                        lyr.bevelDirection = typeIDToStringID(d1.getEnumerationValue(s2t('bevelDirection')));
                    d4 = d1.getObjectValue(c2t('TrnS'));
                        lyr.TrnSName = d4.getString(s2t('name'));
                        lyr.antialiasGloss = d1.getBoolean(s2t('antialiasGloss'));
                        lyr.softness = d1.getUnitDoubleValue(s2t('softness'));
                        lyr.useShape = d1.getBoolean(s2t('useShape'));
                        lyr.useTexture = d1.getBoolean(s2t('useTexture'));
                    lyrs.push(lyr);
                };
            };
        }
        else
        {
            lyrs.push(lyr);
        };
        layers++;
    };    
    return lyrs;
};

 

 For example, is there something like this?

 

lyr.parent = d1.getInteger(s2t("layerParent"));

 

Thank you for your time!!!

This topic has been closed for replies.

7 replies

Chuck Uebele
Community Expert
Community Expert
October 4, 2019

I sent you  PM with a dropbox link, for the edit that I did. Not sure if you got that.

Simonetos The Greek
Inspiring
October 6, 2019

Yes of course I got it!!! 🙂 But due to life obligations, I just didn't have the time to test it yet. I'll test it as soon as possible and I'll inform you. Thank you very much for your precious time!!! 🙂

Chuck Uebele
Community Expert
Community Expert
October 6, 2019
Life does happen!
Chuck Uebele
Community Expert
Community Expert
October 2, 2019

Okay, so I took a look at your code. I see the listbox works with getting the layersets, so that you can exclude them. The layer list is blank. I'm assuming that is the what you want done - to populate that layer list and exclude layers from the groups selected?

Simonetos The Greek
Inspiring
October 3, 2019

"The layer list is blank. I'm assuming that is the what you want done - to populate that layer list and exclude layers from the groups selected?"

Not exactly! Everything works in this script except group exclusion. For example, download my test psd file from here, open it and run the script. Then choose "Vector Layer [4]" from "Layer Type" dropdownList control. As you see, you get all document's vectors with bevel and emboss effect. "Play" with checkboxes if you like... You will get different results. The one that doesn't work yet, is to get a refreshed list with all layers except the ones within chosen group name!!! The excluded one(s).

Chuck Uebele
Community Expert
Community Expert
October 3, 2019

Thanks, that will help. One issue I noted, that I haven't really looked into yet, is the listing of the omit folders. You sorted them for the listbox, but a cross reference array needs to be created to know which folder was acually selected. You need to be able to reference the layersets with the item numbers of the listbox. Also, it looks like you put the item numbers on the layer types dropdown list, but, if so, they're off, as none would be [0]. Will see what I can do.

Chuck Uebele
Community Expert
Community Expert
October 1, 2019

Okay, since I don't have all your code, here's what I came up with, and I'm not sure it will work with the rest of your code. What I did is create a UI that has a listbox that displays all the layersets in the document. Select the ones you want to omit. It will then create several arrays: one with the names of the layersets and their parent to make it easier to display in the listbox, one to store the actual layerset - for reference to apply which is omited, and a third to store the layersets that are omited. Towards the end of your code that you provided, I included a loop that compares the parent of layer being tested with the omit group. it's it's omited, the layer isn't pushed into your array. Hope it works. It makes each layer active to check the parent. This might be able to be streamlined, but I didn't know how at this moment not to have the layer selected.

 

#target photoshop
var doc = activeDocument;
var gpSetLayers = [];
var gpSetNames = [];
var omitGp =[]
var run = false;

getGps (doc)//gets the layersets and their names

var dlg = new Window('dialog','Select Layersets to Exclude');
    var lstBx = dlg.add('listbox',undefined,gpSetNames,{numberOfColumns: 1,multiselect: true});
    dlg.btnGp = dlg.add('group');
        dlg.btnGp.orientation = 'row';
        dlg.btnGp.ok = dlg.btnGp.add('button',undefined,'Okay');
        dlg.btnGp.cancel = dlg.btnGp.add('button',undefined,'Cancel');
        
        dlg.btnGp.ok.onClick = function(){
            run = true;
            for(var i=0;i<lstBx.items.length;i++){
                if(lstBx.items[i].selected){omitGp.push(gpSetLayers[i])}
                    }//end for loop     
                dlg.close();
                }//end function
            
dlg.show();

function getGps(gp){//gets layersets and their names
    for(var i=0;i<gp.layers.length;i++){
        if(gp.layers[i].typename == 'LayerSet'){
            gpSetLayers.push(gp.layers[i]);
            gpSetNames.push(gp.layers[i].name + ' Parent: ' + gp.layers[i].parent.name);
            getGps (gp.layers[i])
            }//end if
        }//end for loop
    }//end function

function getLayerFromID(layId){//Selects layers based on ID
    var idslct = charIDToTypeID( "slct" );
        var desc16 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref4 = new ActionReference();
            var idLyr = charIDToTypeID( "Lyr " );
            ref4.putIdentifier( idLyr, layId );
        desc16.putReference( idnull, ref4 );
        var idMkVs = charIDToTypeID( "MkVs" );
        desc16.putBoolean( idMkVs, false );
        var idLyrI = charIDToTypeID( "LyrI" );
            var list2 = new ActionList();
            list2.putInteger( 2 );
        desc16.putList( idLyrI, list2 );
    executeAction( idslct, desc16, DialogModes.NO );    
    }

function getLayersData()
{
    var lyrs = [];
    var layers = 1;
    while (true)
    {
        ref = new ActionReference();
        ref.putIndex(charIDToTypeID('Lyr '), layers);
        try
        {
            var d1 = executeActionGet(ref);
            var d2 = executeActionGet(ref);
            var d3 = executeActionGet(ref);
            var d4 = executeActionGet(ref);
        }
            catch (err)
        {
            break;
        };
        var c2t = function (s){return app.charIDToTypeID(s);};
        var s2t = function (s){return app.stringIDToTypeID(s);};
        var lyr = {};
        lyr.type = d1.getInteger(s2t("layerKind"));
        lyr.name = d1.getString(c2t("Nm  "));
        lyr.id = d1.getInteger(s2t("layerID"));
        if (checkbox1.value == true)
        {
            ref.putEnumerated( c2t("Lyr "), c2t("Ordn"), c2t("Trgt") );
            if (d1.hasKey(s2t('layerEffects')))
            {
                d1 = d1.getObjectValue(s2t('layerEffects'));
                lyr.scale = d1.getUnitDoubleValue(s2t('scale'));
                if (d1.hasKey(s2t('bevelEmboss')))
                {
                    d1 = d1.getObjectValue(s2t('bevelEmboss'));
                        lyr.enabled = d1.getBoolean(s2t('enabled'));
                        lyr.present = d1.getBoolean(s2t('present'));
                        lyr.showInDialog = d1.getBoolean(s2t('showInDialog'));
                        lyr.highlightMode = typeIDToStringID(d1.getEnumerationValue(s2t('highlightMode')));
                    d2 = d1.getObjectValue(s2t('highlightColor'));
                        lyr.red_hlc = d2.getDouble(s2t('red'));
                        lyr.grn_hlc = d2.getDouble(s2t('green'));
                        lyr.blue_hlc = d2.getDouble(s2t('blue'));
                        lyr.highlightOpacity = d1.getUnitDoubleValue(s2t('highlightOpacity'));
                        lyr.shadowMode = typeIDToStringID(d1.getEnumerationValue(s2t('shadowMode')))
                    d3 = d1.getObjectValue(s2t('shadowColor'));
                        lyr.red_sdc = d3.getDouble(s2t('red'));
                        lyr.grn_sdc = d3.getDouble(s2t('green'));
                        lyr.blue_sdc = d3.getDouble(s2t('blue'));
                        lyr.shadowOpacity = d1.getUnitDoubleValue(s2t('shadowOpacity'));
                        lyr.bevelTechnique = typeIDToStringID(d1.getEnumerationValue(s2t('bevelTechnique')));
                        lyr.bevelStyle = typeIDToStringID(d1.getEnumerationValue(s2t('bevelStyle')));
                        lyr.useGlobalAngle = d1.getBoolean(s2t('useGlobalAngle'));
                        lyr.localLightingAngle = d1.getUnitDoubleValue(s2t('localLightingAngle'));
                        lyr.localLightingAltitude = d1.getUnitDoubleValue(s2t('localLightingAltitude'));
                        lyr.strengthRatio = d1.getUnitDoubleValue(s2t('strengthRatio'));
                        lyr.blur = d1.getUnitDoubleValue(s2t('blur'));
                        lyr.bevelDirection = typeIDToStringID(d1.getEnumerationValue(s2t('bevelDirection')));
                    d4 = d1.getObjectValue(c2t('TrnS'));
                        lyr.TrnSName = d4.getString(s2t('name'));
                        lyr.antialiasGloss = d1.getBoolean(s2t('antialiasGloss'));
                        lyr.softness = d1.getUnitDoubleValue(s2t('softness'));
                        lyr.useShape = d1.getBoolean(s2t('useShape'));
                        lyr.useTexture = d1.getBoolean(s2t('useTexture'));
                        
//////////////////////////////////////////////////Add loop to get parent/////////////////////////////////////////////////
                    var omit = false;
                    for(var i=0;i<omitGp.length;i++){
                        if(getLayerFromID (layers).parent == omitGp[i]){omit = true}
                        }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////                    
                    if(!omit){lyrs.push(lyr)};
                };
            };
        }
        else
        {
            lyrs.push(lyr);
        };
        layers++;
    };    
    return lyrs;
};
Simonetos The Greek
Inspiring
October 2, 2019

Well, your code is working, but individually!!!

My first "problem" is that my document has so many groups, that script takes over one minute to list all of them!!! My second "problem" is that I can't adjust your code's logic into my code. I have to change so many things that I think "I'll lose the ball". As you have already understand, I'm new to all this. So, I think it's better to send you my code so you to have a better view of what I am trying to do. You can download my full code from here.

 

Thank you so much for your time!!!

Chuck Uebele
Community Expert
Community Expert
October 2, 2019
I'll see what I can do.
Chuck Uebele
Community Expert
Community Expert
October 1, 2019

Just to be clear, do you know what layer set you want to exclude at the time of running the script? So in your example, you have a "vector 1" shape in layerset 5, but you don't want it listed? Is that because it doesn't have the layer effects that you're looking for, or just that you want to exclude that layerset? If you want to just exclude one layer set that you are aware of at the beginning of running the script, that should be doable.

Simonetos The Greek
Inspiring
October 1, 2019

"Just to be clear, do you know what layer set you want to exclude at the time of running the script?"

Well... Yes, let's say I know what group(s) or layerSet(s) I want to exclude.


"So in your example, you have a "vector 1" shape in layerset 5, but you don't want it listed?"

I have four vectors by the name "Shape 1" within every group (layerSet) and I want to exclude the layerSet by the name "Group 5" for example. Or let's say, the layerSets by the name "Group 5" and "Group 3".

 

"Is that because it doesn't have the layer effects that you're looking for, or just that you want to exclude that layerset?"

The second one, I just want to exclude specific layerSet(s).

Chuck Uebele
Community Expert
Community Expert
September 29, 2019

Okay, I think I needed to just add ".id" after the layer identifier. Still haven't tested it.

function getLayersData()
{
    var curLayerSet = app.activeDocument.activeLayer;
    var lyrs = [];
    var layers = 1;
    for(var i=0;i<curLayerSet.length;i++)
    //while (true)
    {
        ref = new ActionReference();
        ref.putIndex(charIDToTypeID('Lyr '), curLayerSet[i].id);
//        ref.putIndex(charIDToTypeID('Lyr '), layers);
        try
        {
            var d1 = executeActionGet(ref);
            var d2 = executeActionGet(ref);
            var d3 = executeActionGet(ref);
            var d4 = executeActionGet(ref);
        }
            catch (err)
        {
            break;
        };
        var c2t = function (s){return app.charIDToTypeID(s);};
        var s2t = function (s){return app.stringIDToTypeID(s);};
        var lyr = {};
        lyr.type = d1.getInteger(s2t("layerKind"));
        lyr.name = d1.getString(c2t("Nm  "));
        lyr.id = d1.getInteger(s2t("layerID"));
        if (checkbox1.value == true)
        {
            ref.putEnumerated( c2t("Lyr "), c2t("Ordn"), c2t("Trgt") );
            if (d1.hasKey(s2t('layerEffects')))
            {
                d1 = d1.getObjectValue(s2t('layerEffects'));
                lyr.scale = d1.getUnitDoubleValue(s2t('scale'));
                if (d1.hasKey(s2t('bevelEmboss')))
                {
                    d1 = d1.getObjectValue(s2t('bevelEmboss'));
                        lyr.enabled = d1.getBoolean(s2t('enabled'));
                        lyr.present = d1.getBoolean(s2t('present'));
                        lyr.showInDialog = d1.getBoolean(s2t('showInDialog'));
                        lyr.highlightMode = typeIDToStringID(d1.getEnumerationValue(s2t('highlightMode')));
                    d2 = d1.getObjectValue(s2t('highlightColor'));
                        lyr.red_hlc = d2.getDouble(s2t('red'));
                        lyr.grn_hlc = d2.getDouble(s2t('green'));
                        lyr.blue_hlc = d2.getDouble(s2t('blue'));
                        lyr.highlightOpacity = d1.getUnitDoubleValue(s2t('highlightOpacity'));
                        lyr.shadowMode = typeIDToStringID(d1.getEnumerationValue(s2t('shadowMode')))
                    d3 = d1.getObjectValue(s2t('shadowColor'));
                        lyr.red_sdc = d3.getDouble(s2t('red'));
                        lyr.grn_sdc = d3.getDouble(s2t('green'));
                        lyr.blue_sdc = d3.getDouble(s2t('blue'));
                        lyr.shadowOpacity = d1.getUnitDoubleValue(s2t('shadowOpacity'));
                        lyr.bevelTechnique = typeIDToStringID(d1.getEnumerationValue(s2t('bevelTechnique')));
                        lyr.bevelStyle = typeIDToStringID(d1.getEnumerationValue(s2t('bevelStyle')));
                        lyr.useGlobalAngle = d1.getBoolean(s2t('useGlobalAngle'));
                        lyr.localLightingAngle = d1.getUnitDoubleValue(s2t('localLightingAngle'));
                        lyr.localLightingAltitude = d1.getUnitDoubleValue(s2t('localLightingAltitude'));
                        lyr.strengthRatio = d1.getUnitDoubleValue(s2t('strengthRatio'));
                        lyr.blur = d1.getUnitDoubleValue(s2t('blur'));
                        lyr.bevelDirection = typeIDToStringID(d1.getEnumerationValue(s2t('bevelDirection')));
                    d4 = d1.getObjectValue(c2t('TrnS'));
                        lyr.TrnSName = d4.getString(s2t('name'));
                        lyr.antialiasGloss = d1.getBoolean(s2t('antialiasGloss'));
                        lyr.softness = d1.getUnitDoubleValue(s2t('softness'));
                        lyr.useShape = d1.getBoolean(s2t('useShape'));
                        lyr.useTexture = d1.getBoolean(s2t('useTexture'));
                    lyrs.push(lyr);
                };
            };
        }
        else
        {
            lyrs.push(lyr);
        };
//        layers++;
    };    
    return lyrs;
};
Simonetos The Greek
Inspiring
October 1, 2019

First of all thank you for your answer!!!

 

I tested your code... It is functional in someway, but it can't be used in my case. What I want to do, is to list all document's layers, excluding those which "folds" into a specific group, with specific name!!!

 

For example, let's say we have five groups by the names Group1, 2, 3, 4 and 5.


In each group we have four vectors with the same name. "Shape 1", all of them.

 

I want to list all these "Shape 1" vectors, except the ones within Group 5!!!


This is why I need layer's parent name!!! I have found the way to list all group names into another function... So I just need this function, the one I am showing, to check the parent name for every layer and if layer is a child of this group to be excluded!!!

 

Thank you for your time!!!

Chuck Uebele
Community Expert
Community Expert
September 29, 2019

I think I made a mistake. The for loop has to use the layer ID number and not the layer itself. A routine has to be added to get that number.

Chuck Uebele
Community Expert
Community Expert
September 29, 2019

Try changing the while loop to a for loop, using the children of the layerset. I haven't tested this, but it should work. Just select the layerset you want to check, before you run the code. It will not find nested layersets.

 

function getLayersData()
{
    var curLayerSet = app.activeDocument.activeLayer;
    var lyrs = [];
    var layers = 1;
    for(var i=0;i<curLayerSet.length;i++)
    //while (true)
    {
        ref = new ActionReference();
        ref.putIndex(charIDToTypeID('Lyr '), curLayerSet[i]);
//        ref.putIndex(charIDToTypeID('Lyr '), layers);
        try
        {
            var d1 = executeActionGet(ref);
            var d2 = executeActionGet(ref);
            var d3 = executeActionGet(ref);
            var d4 = executeActionGet(ref);
        }
            catch (err)
        {
            break;
        };
        var c2t = function (s){return app.charIDToTypeID(s);};
        var s2t = function (s){return app.stringIDToTypeID(s);};
        var lyr = {};
        lyr.type = d1.getInteger(s2t("layerKind"));
        lyr.name = d1.getString(c2t("Nm  "));
        lyr.id = d1.getInteger(s2t("layerID"));
        if (checkbox1.value == true)
        {
            ref.putEnumerated( c2t("Lyr "), c2t("Ordn"), c2t("Trgt") );
            if (d1.hasKey(s2t('layerEffects')))
            {
                d1 = d1.getObjectValue(s2t('layerEffects'));
                lyr.scale = d1.getUnitDoubleValue(s2t('scale'));
                if (d1.hasKey(s2t('bevelEmboss')))
                {
                    d1 = d1.getObjectValue(s2t('bevelEmboss'));
                        lyr.enabled = d1.getBoolean(s2t('enabled'));
                        lyr.present = d1.getBoolean(s2t('present'));
                        lyr.showInDialog = d1.getBoolean(s2t('showInDialog'));
                        lyr.highlightMode = typeIDToStringID(d1.getEnumerationValue(s2t('highlightMode')));
                    d2 = d1.getObjectValue(s2t('highlightColor'));
                        lyr.red_hlc = d2.getDouble(s2t('red'));
                        lyr.grn_hlc = d2.getDouble(s2t('green'));
                        lyr.blue_hlc = d2.getDouble(s2t('blue'));
                        lyr.highlightOpacity = d1.getUnitDoubleValue(s2t('highlightOpacity'));
                        lyr.shadowMode = typeIDToStringID(d1.getEnumerationValue(s2t('shadowMode')))
                    d3 = d1.getObjectValue(s2t('shadowColor'));
                        lyr.red_sdc = d3.getDouble(s2t('red'));
                        lyr.grn_sdc = d3.getDouble(s2t('green'));
                        lyr.blue_sdc = d3.getDouble(s2t('blue'));
                        lyr.shadowOpacity = d1.getUnitDoubleValue(s2t('shadowOpacity'));
                        lyr.bevelTechnique = typeIDToStringID(d1.getEnumerationValue(s2t('bevelTechnique')));
                        lyr.bevelStyle = typeIDToStringID(d1.getEnumerationValue(s2t('bevelStyle')));
                        lyr.useGlobalAngle = d1.getBoolean(s2t('useGlobalAngle'));
                        lyr.localLightingAngle = d1.getUnitDoubleValue(s2t('localLightingAngle'));
                        lyr.localLightingAltitude = d1.getUnitDoubleValue(s2t('localLightingAltitude'));
                        lyr.strengthRatio = d1.getUnitDoubleValue(s2t('strengthRatio'));
                        lyr.blur = d1.getUnitDoubleValue(s2t('blur'));
                        lyr.bevelDirection = typeIDToStringID(d1.getEnumerationValue(s2t('bevelDirection')));
                    d4 = d1.getObjectValue(c2t('TrnS'));
                        lyr.TrnSName = d4.getString(s2t('name'));
                        lyr.antialiasGloss = d1.getBoolean(s2t('antialiasGloss'));
                        lyr.softness = d1.getUnitDoubleValue(s2t('softness'));
                        lyr.useShape = d1.getBoolean(s2t('useShape'));
                        lyr.useTexture = d1.getBoolean(s2t('useTexture'));
                    lyrs.push(lyr);
                };
            };
        }
        else
        {
            lyrs.push(lyr);
        };
//        layers++;
    };    
    return lyrs;
};