Skip to main content
Shulipa Bernad
Inspiring
January 8, 2019
Answered

How to Find Selected Layers and Run Events

  • January 8, 2019
  • 2 replies
  • 4103 views

Greetings everyone!

Friends, I'm looking for a script that is able to find only the selected layers and run this script:

try { 

        var tranf = new ActionDescriptor()

        executeAction( charIDToTypeID( "Trnf" ), tranf , DialogModes.ALL );

        executeAction( charIDToTypeID( "Dstt" ), undefined, DialogModes.NO );

        } catch(e) {} 

If there is only one selected layer, run my script .....

If there are two or more layers selected, run my script in layer order.

Any help is very important.

This topic has been closed for replies.
Correct answer Kukurykus

That was proposition for a start, to display by alert selected layers in the array. To do that you want, just use the script you pasted (original) and then comment an alert I mentioned, plus add following code. It works for layers with unique names.

sTT = stringIDToTypeID;

(ref = new ActionReference()).putEnumerated(sTT('layer'), sTT('ordinal'), sTT('front'));

(dsc = new ActionDescriptor()).putReference(sTT('null'), ref), executeAction(sTT('select'), dsc)

for(i = 0; i < GroupedLayers.length;) {

     aD.activeLayer = (aD = activeDocument).layers.getByName(GroupedLayers[i++].name)

     executeAction(sTT('transform'), dsc , DialogModes.ALL), executeAction(sTT('desaturate'))

}

2 replies

Geppetto Luis
Legend
January 9, 2019

See if this is good for you.

app.bringToFront(); 

main(); 

function main(){ 

if(!documents.length) return; 

var selLayers=[]; 

selLayers= getSelectedLayersIdx(); 

for (var a in selLayers){ 

    makeActiveByIndex( selLayers, false ); 

///// YOUR SCRIPT

  

try {   

        var tranf = new ActionDescriptor()  

        executeAction( charIDToTypeID( "Trnf" ), tranf , DialogModes.ALL ); 

        executeAction( charIDToTypeID( "Dstt" ), undefined, DialogModes.NO ); 

        } catch(e) {}  

///// END  YOUR SCRIPT

 

    } 

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;  

}; 

function makeActiveByIndex( idx, visible ){  

    var desc = new ActionDescriptor();  

      var ref = new ActionReference();  

      ref.putIndex(charIDToTypeID( "Lyr " ), idx)  

      desc.putReference( charIDToTypeID( "null" ), ref );  

      desc.putBoolean( charIDToTypeID( "MkVs" ), visible );  

   executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );  

}; 

Shulipa Bernad
Inspiring
January 10, 2019

geppettol66959005 , That's exactly what I'm after. Thank you friend.

Kukurykus
Legend
January 10, 2019

Didn't my solution work for you as that do exactly same?

Kukurykus
Legend
January 8, 2019
Shulipa Bernad
Inspiring
January 9, 2019

This solution does not work with my script. In it there is a "Ctrl + T" transformation function that I need to individually resize each layer with specific sizes.

I tried this,

var GroupedLayers = []; 

GroupedLayers = GetSelectedLayers(); 

for(var z in GroupedLayers){ 

    //alert(GroupedLayers.name); 

   /////////////////////////////////////////

  try {   

        var tranf = new ActionDescriptor()  

        executeAction( charIDToTypeID( "Trnf" ), tranf , DialogModes.ALL ); 

        executeAction( charIDToTypeID( "Dstt" ), undefined, DialogModes.NO ); 

        } catch(e) {}  

    ////////////////////////////////////

    } 

function GetSelectedLayers() { 

var A=[]; 

    var desc11 = new ActionDescriptor(); 

        var ref9 = new ActionReference(); 

        ref9.putClass( stringIDToTypeID('layerSection') ); 

    desc11.putReference( charIDToTypeID('null'), ref9 ); 

        var ref10 = new ActionReference(); 

        ref10.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); 

    desc11.putReference( charIDToTypeID('From'), ref10 ); 

    executeAction( charIDToTypeID('Mk  '), desc11, DialogModes.NO ); 

var gL = activeDocument.activeLayer.layers; 

for(var i=0;i<gL.length;i++){  

A.push(gL);  

}  

executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO ); 

return A; 

};

Does not work: It is critical that the event be applied individually on each layer if more than one is selected

Kukurykus
KukurykusCorrect answer
Legend
January 9, 2019

That was proposition for a start, to display by alert selected layers in the array. To do that you want, just use the script you pasted (original) and then comment an alert I mentioned, plus add following code. It works for layers with unique names.

sTT = stringIDToTypeID;

(ref = new ActionReference()).putEnumerated(sTT('layer'), sTT('ordinal'), sTT('front'));

(dsc = new ActionDescriptor()).putReference(sTT('null'), ref), executeAction(sTT('select'), dsc)

for(i = 0; i < GroupedLayers.length;) {

     aD.activeLayer = (aD = activeDocument).layers.getByName(GroupedLayers[i++].name)

     executeAction(sTT('transform'), dsc , DialogModes.ALL), executeAction(sTT('desaturate'))

}