Skip to main content
smithcgl9043167
Inspiring
January 7, 2019
Answered

Check selection of layers

  • January 7, 2019
  • 1 reply
  • 2168 views

How do I check if I have two layers selected?

If you only have one or baby, trigger an alert message: "You need to select two layers!"

Thanks for the support

This topic has been closed for replies.
Correct answer r-bin

r-bin  escreveu

Do you just need to know if two layers are selected or do you need to get two objects of type Layer (ArtLayer) or is there enough only layers IDs?

I just need to know if two layers are selected, if true, a scpit will be executed, if not; that there is an alert message and do nothing.


alert(ckeck_2_selected())

function ckeck_2_selected()

    {

    try {

        var r = new ActionReference();   

        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayers"));

        r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

        var d = executeActionGet(r);

        if (!d.hasKey(stringIDToTypeID("targetLayers"))) return false;

        var l = d.getList(stringIDToTypeID("targetLayers"));

        if (l.count != 2) return false;

        var n = 0;

        try { activeDocument.backgroundLayer } catch (e) { n = 1; }

       

        for (var i = 0; i < 2; i++)

            {

            var r = new ActionReference();

            r.putIndex(stringIDToTypeID("layer"), l.getReference(i).getIndex() + n);

            if (executeActionGet(r).getEnumerationValue(stringIDToTypeID("layerSection")) != stringIDToTypeID("layerSectionContent")) return false;

            }

        return true;

        }

    catch(e) { alert(e); throw e; }

    }

1 reply

Kukurykus
Legend
January 7, 2019
smithcgl9043167
Inspiring
January 7, 2019

This is a good example, but I have not been able to modify it to make it capable of working the way I need it, I may not have been more specific in the question:

Does this image help you to understand? Thank you

It would be somewhat similar to checking for selected layers of this script. But this code is very difficult to understand.

https://pastebin.com/raw/e0DzJkUA

smithcgl9043167
Inspiring
January 7, 2019

Gave work but solved it! Thanks to this same script:

https://pastebin.com/raw/e0DzJkUA

Final result:

main();

function main(){

    var desc = new ActionDescriptor();

    var ref = new ActionReference();

    ref.putClass( stringIDToTypeID( "layerSection" ) );

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

    var ref2 = new ActionReference();

    ref2.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

    desc.putReference( charIDToTypeID( "From" ), ref2 );

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

    var groupLayers = activeDocument.activeLayer.layers

    var selectedLayers = new Array;

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

   selectedLayers.push(groupLayers);

}

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

if(selectedLayers.length == 0) return;

app.activeDocument.activeLayer = selectedLayers[selectedLayers.length - 1];

if(selectedLayers.length != 2) {

alert("You need to select two layers!")

return;

}

     alert ("OK! Run an Event!")

}