Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


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

Community Beginner ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

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

TOPICS
Expressions, Scripting

Views

54

Likes

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

correct answers 1 Correct answer

Adobe Community Professional , Aug 12, 2021 Aug 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" }

Likes

Translate

Translate
Adobe Community Professional ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

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"

}

Likes

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
Community Beginner ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

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.

Likes

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 Professional ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

LATEST

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.

Likes

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