Skip to main content
Participant
April 23, 2020
Question

Layers Selection: Invert Selected Layers

  • April 23, 2020
  • 1 reply
  • 191 views

Say I have layer 1 to layer 10.
I selected layers 1 & 3 and wanted to select layers other than 1 & 3.
Bascially, I want to select layers: 2,4,5,6...10.

Is there a way to do that?

Thanks!

This topic has been closed for replies.

1 reply

c.pfaffenbichler
Community Expert
Community Expert
April 23, 2020

Scripting. 

Do you have JavaScript experience? 

 

But the issue may become way more complicated once Groups, Clipping Masks, Adjustment Layers, … are in the mix. 

Calvin.CAuthor
Participant
April 23, 2020

hah, I have 0 scripting experience.
Yea I was hoping there is a shortcut.

right now, my method is:
linked the layers after selecting them.
attribute> reveal not linked layers. 
not linked layers revealed> select all

c.pfaffenbichler
Community Expert
Community Expert
April 23, 2020

You could try this: 

// based on code by paul riggott;
// 2020, use it at your own risk;
app.bringToFront();
main();
function main(){
if(!documents.length) return;
var selLayers=[];
selLayers= getSelectedLayersIdx();
// get number of layers;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
var hasBackground = applicationDesc.getBoolean(stringIDToTypeID("hasBackgroundLayer"));
if (hasBackground == true) {var theStart = 0}
else {var theStart = 1};
// deselect layers;
var desc2 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated( stringIDToTypeID( "layer" ), stringIDToTypeID( "ordinal" ), stringIDToTypeID( "targetEnum" ) );
desc2.putReference( stringIDToTypeID( "null" ), ref1 );
executeAction( stringIDToTypeID( "selectNoLayers" ), desc2, DialogModes.NO );
// select;
var thisIndex = 0;
var thisNumber = selLayers[thisIndex];
for (var m = theStart; m <= theNumber; m++) {
   if (m == thisNumber) {
      thisIndex++;
      var thisNumber = selLayers[thisIndex];
      }
   else {
      selectLayerByIndex(m,true)
   }
};
};
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++){ 
            try{ 
               activeDocument.backgroundLayer; 
               selectedLayers.push(  desc.getReference( i ).getIndex() ); 
            }catch(e){ 
               selectedLayers.push(  desc.getReference( i ).getIndex()+1 ); 
            } 
          } 
       }else{ 
         var ref = new ActionReference(); 
         ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" )); 
         ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
         try{ 
            activeDocument.backgroundLayer; 
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1); 
         }catch(e){ 
            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))); 
         } 
      } 
      return selectedLayers; 
};
// based on code by mike hale, via paul riggott;
function selectLayerByIndex(id,add){ 
   add = undefined ? add = false:add 
   var ref = new ActionReference();
       ref.putIndex(charIDToTypeID("Lyr "), id);
       var desc = new ActionDescriptor();
       desc.putReference(charIDToTypeID("null"), ref );
          if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
         desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
      try{
       executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
   }catch(e){
   alert(e.message); 
   }
   };