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

Is there a selected layer or not?

Contributor ,
Sep 02, 2016 Sep 02, 2016

Copy link to clipboard

Copied

Hi all,

I need a little function which is telling me if there is a selected layer or not. Now I always get back the active layer even if it isn't selected with this AM Code:

getSelectedLayerId: function()

{

  var ref = new ActionReference();

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

  return executeActionGet(ref).getInteger(stringIDToTypeID( "layerID" ));

},

Thank You in advance!

TOPICS
Actions and scripting

Views

967

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

Guide , Sep 02, 2016 Sep 02, 2016

I believe this has been fixed in the latest version of Photoshop, but I dont't have it so not sure.

Up to the latest you could use the following code as it checks to see if a single layer is selected by checking the layers visibility.

function getSelectedLayersIdx(){

      var selectedLayers = new Array();

      var backGroundCounter = 1;

            if(activeDocument.artLayers.length > 0){

            backGroundCounter = activeDocument.artLayers[activeDocument.artLayers.length - 1].isBackgroundLaye

...

Votes

Translate

Translate
Adobe
Guide ,
Sep 02, 2016 Sep 02, 2016

Copy link to clipboard

Copied

I believe this has been fixed in the latest version of Photoshop, but I dont't have it so not sure.

Up to the latest you could use the following code as it checks to see if a single layer is selected by checking the layers visibility.

function getSelectedLayersIdx(){

      var selectedLayers = new Array();

      var backGroundCounter = 1;

            if(activeDocument.artLayers.length > 0){

            backGroundCounter = activeDocument.artLayers[activeDocument.artLayers.length - 1].isBackgroundLayer ? 0 : 1;

            }

      var ref = new ActionReference();

      ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("targetLayers"));

      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++){

               selectedLayers.push(  desc.getReference( i ).getIndex() +backGroundCounter );

          }

      if(app.version.match(/^\d+/) > 15) return selectedLayers ;

       }else{

           if(app.version.match(/^\d+/) > 15) return selectedLayers ;

         var ref = new ActionReference();

         ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));

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

         if(!backGroundCounter){

            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )) -1);

            }else{

                selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));

                }

     var vis = app.activeDocument.activeLayer.visible;

        if(vis == true) app.activeDocument.activeLayer.visible = false;

        var desc9 = new ActionDescriptor();

    var list9 = new ActionList();

    var ref9 = new ActionReference();

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

    list9.putReference( ref9 );

    desc9.putList( charIDToTypeID("null"), list9 );

    executeAction( charIDToTypeID("Shw "), desc9, DialogModes.NO );

    if(app.activeDocument.activeLayer.visible == false) selectedLayers.shift();

        app.activeDocument.activeLayer.visible = vis;

      }

      return selectedLayers;

};

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
Contributor ,
Sep 02, 2016 Sep 02, 2016

Copy link to clipboard

Copied

Thank You SuperMerlin! It is working well.

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 ,
Mar 13, 2017 Mar 13, 2017

Copy link to clipboard

Copied

LATEST

SuperMerlin , it hasn't been fixed (at least not until CC 2017) – if you try with basic DOM code such as

app.activeDocument.activeLayer

it keeps pointing to a layer even if none is active.

Davide

Davide Barranca - PS developer and author
www.ps-scripting.com

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