Quantcast
Channel: Adobe Community: Message List - Photoshop Scripting
Viewing all articles
Browse latest Browse all 27456

Get selected group and layers?

$
0
0

Hi, I need to get the selected layers and groups in my script. I found this  https://forums.adobe.com/message/2666611.

 

here is the code:

 

function getSelectedLayers() 
{      var doc = app.activeDocument;      var selIndices = getSelectedLayersIdx();      var selLayers = new Array();            if (selIndices.length == 1)      {           selLayers.push(doc.activeLayer);      }      else      {            for (var i=0; i<selIndices.length; i++)           {                if (makeActiveByIndex(selIndices[i], false) != -1)                     selLayers.push(doc.activeLayer);                     }      }            return selLayers;      
}  
function getSelectedLayersIdx() 
{      var selectedLayers = new Array;      var ref = new ActionReference();      ref.putEnumerated(charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));      var desc = executeActionGet(ref);      if (desc.hasKey(stringIDToTypeID('targetLayers')))      {           desc = desc.getList(stringIDToTypeID('targetLayers'));           var c = desc.count;            var selectedLayers = new Array();           for (var i=0; i<c; i++)           {                selectedLayers.push(desc.getReference(i).getIndex());           }      }      else      {           var ref = new ActionReference();            ref.putProperty(charIDToTypeID('Prpr'), charIDToTypeID('ItmI'));            ref.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));           selectedLayers.push(executeActionGet(ref).getInteger(charIDToTypeID('ItmI')));      }      return selectedLayers; 
}  
function makeActiveByIndex(idx, forceVisible) 
{      try      {           var desc = new ActionDescriptor();           var ref = new ActionReference();           ref.putIndex(charIDToTypeID('Lyr '), idx)           desc.putReference(charIDToTypeID('null'), ref);           desc.putBoolean(charIDToTypeID('MkVs'), forceVisible);           executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);      }      catch(e)      {           return -1;      } 
} 

 

 

 

 

 

This works for the layers or when I am trying to select only 1 group. However when I am selecting multiple groups, for some reason it selects the top child layer rather than the group itself. I don't really know much about the ActionManager and can't really understand what is causing the problem.


Viewing all articles
Browse latest Browse all 27456

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>