5 Replies Latest reply on Apr 4, 2016 10:16 AM by wutronic

    get activeLayer index

    wutronic

      I can't seem to get the activeLayer by index. I see that artLayers have an index, but activeLayer seems to only have "itemIndex" which doesn't seem to do same thing.

        • 1. Re: get activeLayer index
          Chuck Uebele Adobe Community Professional & MVP

          Do you want to make a layer active by using it's index or get the active layer's index?

           

          To get a layer by it's indes:

           

          function makeSingleActiveByIndex(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);
          
               
          };//end makeSingleActiveByIndex
          
          

           

          To get the index of selected layers:

           

          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 {
                          docRef.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 {
                      docRef.backgroundLayer;
                      selectedLayers.push(executeActionGet(ref).getInteger(charIDToTypeID("ItmI")) - 1);
                  } catch (e) {
                      selectedLayers.push(executeActionGet(ref).getInteger(charIDToTypeID("ItmI")));
                  }
              }
              return selectedLayers;
          }
          
          
          • 2. Re: get activeLayer index
            wutronic Level 1

            Sorry I meant get index of the activeLayer.

            • 3. Re: get activeLayer index
              Chuck Uebele Adobe Community Professional & MVP

              Then use the second function I posted.

              • 4. Re: get activeLayer index
                Chuck Uebele Adobe Community Professional & MVP

                If you're not already aware of this, the layer's indexes change depending if there is a background layer. If I recall properly, the BG layer is 0 if there is no bg layer, the numbering starts at 1.

                • 5. Re: get activeLayer index
                  wutronic Level 1

                  ah, good to know. Thanks!