select layer by index?

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

buga

select layer by index?

Post by buga »

will see, I make this post in relation to another post, because I had given the post by closed, but I found a bug and I can not fix it.

pfaffenbichler, gave me this code, which generates a list of all layers, select a layer and makes an alert. (The code works great)
The problem is that I would like to activate the selected layer.

How do I prevent errors if the name of the layer is doubled?

I'm new programming scripts and I do not know how to fix it

Code: Select all// 2012, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
////////////////////////////////////
// get document-path and -title;
   var myDocument = app.activeDocument;
   try {var myDocName = myDocument.name.match(/(.*)\.[^\.]+$/)[1]
      var myPath = myDocument.path}
   catch (e) {var myDocName = myDocument.name
      var myPath = "~/Desktop"};
   var theLayers = collectLayers(myDocument, []);
////////////////////////////////////
// create the dialog;   
   var dlg = new Window('dialog', "select layer", [500,300,930,800]);   
//create list for layer-selection;
   dlg.layerRange = dlg.add('panel', [21,20,279,445], "select layer");
   dlg.layerRange.layersList = dlg.layerRange.add('listbox', [11,20,240,405], '', {multiselect: false});
   for (var q = 0; q < theLayers.length; q++) {
      dlg.layerRange.layersList.add ("item", theLayers[q].name);
      };
   dlg.layerRange.layersList.items[0].selected = true;
// ok- and cancel-buttons;
   dlg.buildBtn = dlg.add('button', [220,460,410,485], 'OK', {name:'ok'});
   dlg.cancelBtn = dlg.add('button', [21,460,210,485], 'Cancel', {name:'cancel'});
   dlg.center();
////////////////////////////////////
   var myReturn = dlg.show ();
// in case of OK;
   if (myReturn == 1) {
// get the number instead of the name;
      for (var p = 0; p < dlg.layerRange.layersList.items.length; p++) {
         if (dlg.layerRange.layersList.items[p].selected == true) {
            var theTarget = theLayers[p];
            }
         };
      alert (theTarget);
      }
   };
else {alert ("no document open")};
////////////////////////////////////
////// function collect all layers //////
function collectLayers (theParent, allLayers) {
   if (!allLayers) {var allLayers = new Array}
   else {};
   for (var m = theParent.layers.length - 1; m >= 0;m--) {
      var theLayer = theParent.layers[m];
// apply the function to layersets;
      if (theLayer.typename == "ArtLayer") {
         allLayers.push(theLayer)
         }
      else {
// this line includes the layer groups;
         allLayers.push(theLayer);
         collectLayers(theLayer, allLayers)
         }
      }
   return allLayers
};

Could someone help me or guide me to fix it please?

would be very grateful.

thanks

greetings

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

pfaffenbichler

select layer by index?

Post by pfaffenbichler »

If you change the alert-line to
Code: Select all     app.activeDocument.activeLayer = theTarget;
does an error occur?
buga

select layer by index?

Post by buga »

Oops. It works.

I'm so sorry. I selected the layer by name.

Eliminate the post if you want.

Pfaffenbichler, many thanks and sorry for my ignorance.

regards
pfaffenbichler

select layer by index?

Post by pfaffenbichler »

No problem; the regulars on this Forum have taught me a lot about Scripting, so I’m trying to pass on the favour.

Like I said in the other post this approach (using the Document Object Model) will probably be slower than an Action Manager approach – if you notice a serious lag with documents with many layers you can give that a try sometime.