Photoshop: function "getSelectedLayers()" in scripts doesn't work

  • 1
  • Problem
  • Updated 2 years ago
  • (Edited)
 function getSelectedLayers() {     var selectedLayers = [];
     try {
         var backGroundCounter = activeDocument.artLayers[
                 activeDocument.artLayers.length - 1].isBackgroundLayer ? 0 : 1;
         var ref = new ActionReference();
         var keyTargetLayers = app.stringIDToTypeID('targetLayers');
         ref.putProperty(app.charIDToTypeID('Prpr'), keyTargetLayers);
         ref.putEnumerated(app.charIDToTypeID('Dcmn'), app.charIDToTypeID(
             'Ordn'), app.charIDToTypeID('Trgt'));
         var desc = executeActionGet(ref);
         if (desc.hasKey(keyTargetLayers)) {
             var layersList = desc.getList(keyTargetLayers);
             for (var i = 0; i < layersList.count; i++) {
                 var listRef = layersList.getReference(i);
                 selectedLayers.push(listRef.getIndex() +
                     backGroundCounter);
             }
             //hasLayerMask = true;
         }
     } catch (e) {;
         // do nothing 
     }
     return selectedLayers;
 }
activeDocument.artLayers.length - 1].isBackgroundLayer ? 0 : 1; // < -- this line causes crash if all layers are in folder or artboards... so artLayers array is empty 

I found this code in Delete empty layers script which is instaled by default in PS. Probably this function will in the more scripts.
Photo of Jaroslav Bereza

Jaroslav Bereza

  • 781 Posts
  • 166 Reply Likes

Posted 2 years ago

  • 1
Photo of christoph pfaffenbichler

christoph pfaffenbichler, Champion

  • 1225 Posts
  • 172 Reply Likes
Did you just want to point the problem out or do you want to address the issue yourself? 
An additional if-clause should suffice to check whether the document has artLayers at all itself or whether the artLayers all reside in Groups etc. 
Photo of Paul Riggott

Paul Riggott

  • 359 Posts
  • 143 Reply Likes
Would this work?

var backGroundCounter = 1;
if(activeDocument.artLayers.length > 0){
backGroundCounter = activeDocument.artLayers[activeDocument.artLayers.length - 1].isBackgroundLayer ? 0 : 1;
}
Photo of Jaroslav Bereza

Jaroslav Bereza

  • 781 Posts
  • 166 Reply Likes
I tried it now and seems to be working good. :-)
Photo of Paul Riggott

Paul Riggott

  • 359 Posts
  • 143 Reply Likes
I see that the function does not take into count if there is only one layer selected.
Photo of Jaroslav Bereza

Jaroslav Bereza

  • 781 Posts
  • 166 Reply Likes
I am using this function a lot (with my fix) and works good for single layer. Or what do you mean?
Photo of Paul Riggott

Paul Riggott

  • 359 Posts
  • 143 Reply Likes
 desc.getList(keyTargetLayers); 
this will only exist when two or more layers exist
So no check is being done for single or no layer selected.
Photo of Paul Riggott

Paul Riggott

  • 359 Posts
  • 143 Reply Likes
Christoph, has desc.hasKey(keyTargetLayers) changed ? does it now exist if only one layer is selected? if so when did this change cc/2014/2015 etc. ?
In might be nice to amend some of my scripts to suit.
Paul.
Photo of christoph pfaffenbichler

christoph pfaffenbichler, Champion

  • 1224 Posts
  • 172 Reply Likes
With one Layer selected 
var ref = new ActionReference();ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var desc = executeActionGet(ref);
alert (desc.hasKey(stringIDToTypeID( 'targetLayers' )))
gets me »true«. 
Photo of Paul Riggott

Paul Riggott

  • 359 Posts
  • 143 Reply Likes
Thank you Christoph.
Photo of christoph pfaffenbichler

christoph pfaffenbichler, Champion

  • 1224 Posts
  • 172 Reply Likes
You’re welcome. 
Good to see you are still about in Fora.