Skip to main content
Participating Frequently
August 25, 2011
Answered

Getting layer's parent using ActionManager

  • August 25, 2011
  • 2 replies
  • 2552 views

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.

This topic has been closed for replies.
Correct answer Michael_L_Hale

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.

2 replies

Michael_L_HaleCorrect answer
Inspiring
August 25, 2011

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.

Inspiring
August 26, 2011

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

JJMack
Community Expert
Community Expert
August 25, 2011

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

JJMack
SuchysAuthor
Participating Frequently
August 25, 2011

I meant parent == Group (LayerSet)