Getbyname not working

I’m sure I’m doing something wrong but give these docs (https://developer.adobe.com/photoshop/uxp/2022/ps_reference/classes/layers/#getbyname) I assumed that I could just do this to query for a layer:

app.activeDocument.layers.getByName('cool-name')

But it don’t work. Any ideas?

What else are you doing in your code? What are you expecting to happen when you run that code?

var test = app.activeDocument.layers.getByName('cool-name');
console.log(test.id);

You could do something like:

test.selected = true;

But you need to be in exmodal state for that.

Oh, I didn’t realize it had to be inside an exmodal. So some stuff works outside and some only inside of exmodal? Why? And what is the exmodal?

To get something, you don’t need to be in modal state. Only to change something. So getByName() shouldn’t be an issue. But setting layer as selected - is a state change and needs to be executed in modal state

Hey @JasonM ,
I just tried to implement the "getByName(“Name”) and it did not work for me either.
I was executing it in a modal state.
3 years after this initial post. Do we have a solution to this question by now?

Cheers,
Oliver

app.activeDocument.layers returns only top level layers. If you want some layer from sub-tree, you’ll have to search recursively. What console shows for app.activeDocument.layers?

You can get the layer’s json property and use the includes() method to test if it contains the layer name you’re looking for.