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

Catch a subLayer with .getByName(name);

New Here ,
Jul 23, 2020 Jul 23, 2020

Hi, i need (in my extension), to get a subLayers
for example
- firstLayer
-- secondLayer

 

docRef.layers.getByName('firstLayer'); //it's ok
docRef.layers.getByName('secondLayer'); //it's not ok

 

getByName look only at root level's layers
any suggestion?

Thanks

TOPICS
Scripting , SDK , Third party plugins
1.0K
Translate
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
Community Expert ,
Jul 23, 2020 Jul 23, 2020

Does that works for you?

 

app.activeDocument.layers.getByName('firstLayer').layers.getByName('secondLayer').visible = false;

 

 

Layers.layersGetByName.pngexpand image

 

If so

have fun

😉

Translate
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
Guide ,
Jul 23, 2020 Jul 23, 2020

It's shorter and more intuitive (at least for me) to reference names using bracket notation. 

 

var layer2 = app.activeDocument.layers["firstLayer"].layers["secondLayer"];
Translate
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
Enthusiast ,
Jul 24, 2020 Jul 24, 2020

Don't both of the above answers cause the script to fail and stop execution if either layer of this name is not found? Only works for a single depth too.

 

var layer2 = app.activeDocument.layers["firstLayer"].layers["secondLayer"];
// If either "firstLayer" or "secondLayer" don't exist, this alert is never shown:
alert("Test")

 

 

Here's a recursive solution that will snatch any layer or sublayer with any property and return the first instance found:

 

function getLayerByProperty(property, value) {
  Array.prototype.find = function(callback) {
    for (var i = 0; i < this.length; i++)
      if (callback(this[i], i, this)) return this[i];
    return null;
  };
  function get(type, parent) {
    if (arguments.length == 1 || !parent) parent = app.activeDocument;
    var result = [];
    if (!parent[type]) return [];
    for (var i = 0; i < parent[type].length; i++) {
      result.push(parent[type][i]);
      if (parent[type][i][type])
        result = [].concat(result, get(type, parent[type][i]));
    }
    return result || [];
  }
  return get("layers").find(function(layer) {
    return layer[property] == value;
  });
}

 

 

Might seem like overkill but I like this utility because the script never fails silently and it works for any property or any depth, even if this layer is in a chain 9 parent layers in:

 

var target = getLayerByProperty("name", "secondLayer");
// Below alert is always shown. If "secondLayer" does not exist, returns as null
alert(target)

 

 

Translate
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
Guide ,
Jul 25, 2020 Jul 25, 2020
LATEST

In CS6, if a named element is not found, I get "Error 1302 : No such element." pointing to the line. 

 

Thanks for the script. 

 

 

Translate
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