4 Replies Latest reply on Aug 25, 2011 5:18 PM by Michael L Hale

    Getting layer's parent using ActionManager

    Suchys

      Is there a way, to get layer's parent using ActionManager? There is an simple way by layer.parent, but this seems to be rather slow, I would appreciate fast access, but could not get an idea how to do it.

        • 1. Re: Getting layer's parent using ActionManager
          JJMack Most Valuable Participant

          What do you mean by a layer's parent the layer above it, the layer group it may be in, the layers it effects or the layer it is clipped to.  Are layers in a hierarchy? Or someplace within a stack?  You can use the action manger code to go layer forward and backward that will sikp layers with their visibility off and wrap when you reach the top or bottom. I find that usefull.  You can get all layers into an aray with DOM code and run through all layers.

           

           

          As fas as I know the document is the parent of things in it like layers etc

          • 2. Re: Getting layer's parent using ActionManager
            Suchys Level 1

            I meant parent == Group (LayerSet)

            • 3. Re: Getting layer's parent using ActionManager
              Michael L Hale Level 5

              The layer descriptor does not have a 'parent' key. It does have a key with the stringID of "layerSection".

               

              To Action Manger a layerSet is really two layers. One has the value of 'layerSectionStart' in the "layerSection" key. That is the layer that you can see in the Photoshop GUI. It marks the top of a layerSet. The bottom of the layerSet is a hidden layer with the value of "layerSectionEnd" in the "layerSection" key. You can not make that hidden layer active and some of the key have different values than the matching 'top'. For example if the layerSet visible in Photoshop is 'Group 1' then the name for the end of the layerSet is '</Group 1>'.

               

              I think you could find the parent of a layer using Action Manager if you first got the index of the layer in question. Then loop backwards using the index and searching the "layerSection" key for 'layerSectionStart'. If you run into a "layerSectionEnd" before you find the start you need to keep track of that to be sure you find the correct 'start'. Doing something like this is on my list of things to do, I just haven't gotten to it yet.

               

              Altough looping by index in Action Manager is much faster than loop in the DOM, I an not sure that finding the parent with Action Manger would be faster than just making the layer active and using the DOM to find the parent.

              • 4. Re: Getting layer's parent using ActionManager
                Michael L Hale Level 5

                Something like this...

                function getLayerParentAMIndexByAMIndex( idx ){
                     var nestedSets = 0;
                     var layerCount = getNumberOfLayer();
                     for( var l = idx; l <= layerCount; l++ ){
                          var layerSection = getLayerSectionByAMIndex( l );
                          if( layerSection == 'layerSectionEnd' ) nestedSets++;
                          if( layerSection == 'layerSectionStart'  && nestedSets > 0) nestedSets--;
                          if( layerSection == 'layerSectionStart'  && nestedSets == 0) return l;
                     }
                     function getLayerSectionByAMIndex( idx ) { 
                          var ref = new ActionReference(); 
                          ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID('layerSection')); 
                          ref.putIndex( charIDToTypeID( "Lyr " ), idx );
                          return typeIDToStringID(executeActionGet(ref).getEnumerationValue(stringIDToTypeID('layerSection'))); 
                     };
                     function getNumberOfLayer(){ 
                          var ref = new ActionReference(); 
                          ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
                          var desc = executeActionGet(ref); 
                          var numberOfLayer = desc.getInteger(charIDToTypeID("NmbL")); 
                          return numberOfLayer; 
                     };
                };
                getLayerNameByIndex( getLayerParentAMIndexByAMIndex( 4 ) );
                function getLayerNameByIndex( idx ) { 
                    var ref = new ActionReference(); 
                    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm  " )); 
                    ref.putIndex( charIDToTypeID( "Lyr " ), idx );
                    return executeActionGet(ref).getString(charIDToTypeID( "Nm  " ));; 
                };
                
                1 person found this helpful