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

Catch a subLayer with .getByName(name);

New Here ,
Jul 23, 2020 Jul 23, 2020

Copy link to clipboard

Copied

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

Views

743

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
Adobe
Community Expert ,
Jul 23, 2020 Jul 23, 2020

Copy link to clipboard

Copied

Does that works for you?

 

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

 

 

Layers.layersGetByName.png

 

If so

have fun

😉

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

Copy link to clipboard

Copied

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

 

var layer2 = app.activeDocument.layers["firstLayer"].layers["secondLayer"];

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
Enthusiast ,
Jul 24, 2020 Jul 24, 2020

Copy link to clipboard

Copied

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)

 

 

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

Copy link to clipboard

Copied

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. 

 

 

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