• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to Find Selected Layers and Run Events

Engaged ,
Jan 08, 2019 Jan 08, 2019

Copy link to clipboard

Copied

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.

TOPICS
Actions and scripting

Views

3.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 08, 2019 Jan 08, 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;) {

    

...

Votes

Translate

Translate
Adobe
LEGEND ,
Jan 08, 2019 Jan 08, 2019

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 08, 2019 Jan 08, 2019

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 08, 2019 Jan 08, 2019

Copy link to clipboard

Copied

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'))

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jan 09, 2019 Jan 09, 2019

Copy link to clipboard

Copied

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

}; 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 10, 2019 Jan 10, 2019

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 10, 2019 Jan 10, 2019

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 10, 2019 Jan 10, 2019

Copy link to clipboard

Copied

Kukurykus  escreveu

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

I'm still a beginner in scripting! I confess that I tried but I managed to make the modifications and I had already given up. The geppettol66959005 script is easier to understand and I can use it in future tasks

you have adapted my script to your programming mode, I have difficulty understanding your scripts.

In any way I am grateful for your attention Kukurykus you are very kind.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 10, 2019 Jan 10, 2019

Copy link to clipboard

Copied

Okey, thx for answer, but did you try it as I explained what to do (so not the script you created as answer for the links I gave originally)? You simply combine Paul Riggott script with the part I provided the way you paste his part and then below it my part, and that's all. So basicly that you did earlier but much easier way (also commenting an alert).

When you compare the script I wrote with that of geppettol66959005 you will see he does what I did, so putting the part of script you posted in first post into loop. The other thing he does is he selects each layer the transform and saturation have to be applied to.

Just check that and tell me if that worked

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

I am working on a script but I got stuck at one point and Geppetto Luis's script above does almost exactly what I need. This script returns an array of selected layers ordered by their id's. Is it possible to return the array of layers respectively the order of selection? Here is what I mean: I would like to select ony 2 layers. Firstly the Layer_01 and then the Layer_02. If the id of Layer_02 is smaller than that of Layer_01 then the array will look like this: selectedLayers=[Layer_02, Layer_01] but I actually need quite the opposite: selectedLayers=[Layer_01, Layer_02] since Layer_01 was selected in the first place and Layer_02 in the second place . Could anyone please give me any clue how to do that?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

Just read the array in reverse order.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

What if the script above happens to return the array in the correct order based on the id's of the layers arranged in increasing order? I would like to arrange them as they were selected one after the other, in that order.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

Sorry, I did not read your question carefully.

Photoshop does not keep the order of the selection of layers. 

You can use app.notifiers.add ('slct', full path to your script, 'Lyr') - in this case Photoshop will call your script every time a layer is selected and you can keep track of the order in which it was done.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

Well that's what I suspected...it gets even more complicated. Thanks for your reply anyway, jazz-y

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

Why do you need it?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 08, 2021 Mar 08, 2021

Copy link to clipboard

Copied

I select 2 layers (Layer_01 and then Layer_02) to load a channel (red/green/blue) selection from Layer_01 and apply that selection as a mask for Layer_02. Evereything works fine except that I can not return an array of the two selected layers in the order they were selected.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 08, 2021 Mar 08, 2021

Copy link to clipboard

Copied

For two layers, you can mark the target layer, for example, by turning it off (or by assigning a certain color to it, by the way, you can also set the order with colors). After that, the script will determine the desired one among the selected layers, make its own manipulations and possibly turn on the layer or assign to it (them) a none color.

 

You can also try to make an interface with a dialog with a list of current layers. You can control the selection of layers in the list by onChange callback. Then press the "Run" button ("DoIt", "OK") and perform the necessary actions.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 17, 2021 Dec 17, 2021

Copy link to clipboard

Copied

Hi, thanks for sharing this. I ran this by replacing the section with my script, but it was giving me below error. Any idea why? (Kindly note that my script that I pasted doesn't have any errors)

Adams5ED2_0-1639747196679.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 17, 2021 Dec 17, 2021

Copy link to clipboard

Copied

LATEST
makeActiveByIndex(selLayers[a], false)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines