Skip to main content
Inspiring
May 9, 2011
Question

Layers getByName() issue?

  • May 9, 2011
  • 2 replies
  • 8871 views

I'm using CS4 and I'm curious about a particular issue I'm having when I use getByName() to return a layer. It works fine unless the named layer is in a group. Using the following simple code it runs fine until I choose a layer that is within a group. Then it returns an error: No such element.

#target photoshop

n=app.activeDocument.activeLayer.name;

alert(n);

lay=app.activeDocument.artLayers.getByName(n);

alert(lay.name);

Am I doing something wrong? Is there another way other than coding a step-through function to do this so it will return the layer whether it's in a group or not without an error?

This topic has been closed for replies.

2 replies

dserodio
Participating Frequently
March 18, 2012

document.artLayers contains only the "top-level" layers (ie, the ones not inside layer groups). Try using the document.layers container instead, it contains both Art Layers and LayerSets (aka Layer Groups)

Muppet_Mark-QAl63s
Inspiring
May 9, 2011

Using the DOM then I would expect you need to step thru with a recursive function… I would also expect that there is a much faster action manager method have you looked at x's toolkit…?

jugenjuryAuthor
Inspiring
May 9, 2011

I'm afraid you're correct, Mark. The trouble is, the script I'm actually using it in is part of an Events Script and is very time critical. I've spent a lot of effort shaving off even milliseconds in the run time. Now that I've added some grouped layers to my workflow, it's giving me a bit of a problem.

I do have Xbytor's toolkit but have not looked through it as of yet. I was hoping there would be a simple solution using the DOM that would work for any layer so I'm not adding run time to the execution of the script.

Inspiring
May 9, 2011

function makeLayerActiveByName(nm) {
  function cTID(s) { return app.charIDToTypeID(s); };

  try {

    var desc5 = new ActionDescriptor();
        var ref4 = new ActionReference();
        ref4.putName( cTID('Lyr '),  nm);
    desc5.putReference( cTID('null'), ref4 );
    desc5.putBoolean( cTID('MkVs'), false );
    executeAction( cTID('slct'), desc5, DialogModes.NO );

    return true;

} catch (e) {

   return false;

}
};

// usage:

// makeLayerActiveByName("some name");

This shold work. It returns true if a layer with that name exists, false if not. If you have more than one layer with the same name, one of them is selected.