Skip to main content
Bram Hoekstra
Inspiring
August 12, 2021
Answered

How to verify if a layer name has a specific substring?

  • August 12, 2021
  • 1 reply
  • 1693 views

Heey,

 

I'm wondering how to use a substring of a layer name as a condition for an if/else expression.

 

I want to verify that the layer above the layer with the expression, has a specific (part of a) name, and if so, it executes some code.

 

I have added an example. In the example it is only a single word, but I want to be able to apply it to for example "Text Column 1" in such a way that the code will be executed if "Text Column" is in the layer name.

 

I hope my question is clear and that there is someone who can help me.


Thanks in advance

This topic has been closed for replies.
Correct answer Mathias Moehl

I think your attempt

name.substr(...) = "Heading"

has two problems:

1) you need to use == instead of =

2) the second parameter of substr() is the length of the substring you want to get.

So to compare it to "Heading" which has 7 letters, you should do substr(0,7).

 

Extra tip: If you don't want to find it only at the beginning of the comp name, use the function indexOf:

if(name.indexOf("Heading")>=0){

  // name contains "Heading"

}

1 reply

Mathias Moehl
Community Expert
Mathias MoehlCommunity ExpertCorrect answer
Community Expert
August 12, 2021

I think your attempt

name.substr(...) = "Heading"

has two problems:

1) you need to use == instead of =

2) the second parameter of substr() is the length of the substring you want to get.

So to compare it to "Heading" which has 7 letters, you should do substr(0,7).

 

Extra tip: If you don't want to find it only at the beginning of the comp name, use the function indexOf:

if(name.indexOf("Heading")>=0){

  // name contains "Heading"

}

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Bram Hoekstra
Inspiring
August 12, 2021

I tried to adapt my scripting with the tips you gave me, but I get an error that says: "undefined is not an object".

I added a screenshot of the new code.

Mathias Moehl
Community Expert
Community Expert
August 13, 2021

You should run the script in the debugger and see at which line the error occurs exactly. Without a line number, this error message is not helpful.

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects