Skip to main content
SWAX
Inspiring
March 18, 2020
Question

Scripting: detect layer names -- only first result returned

  • March 18, 2020
  • 2 replies
  • 470 views

I have a composition that contains several layers.

Some of the layers are named with "##Q[number]" to mark ##Q1,##Q2,##Q3,##Q4,##Q5 & ##Q6

 

So when I'm trying to detect if the layers are present, I run the following script:

 

for (i = 1; i <= mainComposition.numLayers; i++) {
    var layer = mainComposition.layer(i);
    var findIntroLayer = '##Q' + i;
    if (layer.name === findIntroLayer) {
        $.writeln(findIntroLayer + " found!");
    }
}

 

I only get back:

 

##Q1 found!

 

When I do this in a hard-coded way:

 

if (layer.name == '##Q1') {
    $.writeln("##Q1 found!");
}

if (layer.name === '##Q2') {
    $.writeln("##Q2 found!");
}

...

 

I get back all the layers I'm targeting.

 

Don't know why the dynamically created layer name wouldn't work ... any ideas?

This topic has been closed for replies.

2 replies

Dan Ebberts
Community Expert
Community Expert
March 19, 2020

Your loop will only find layers where the layer name matches the layer number. For example, it will only find ##Q2 if ##Q2 is the second layer. Depending on how you have the layers arranged, you might want to use nested loops so that each layer in the comp gets compared to each possible number.

 

Dan

Mylenium
Legend
March 18, 2020

A value is not a string and vice versa. A simple i.toString() might fix it and of course you need to actually look for the .name before concatenating the string. also I'm not sure what you want to do with the === operator. That's reserved for exact binary matches, not generic compare operations. Lots of small mistakes that may prevent your script from functioning as you want it to...

 

Mylenium