What's the most reliable way to check if a layer is a group?

Is it to check if layer.layers exists or is not undefined, or is an array? Which one? Why isn’t there an isGroup() method? Or did I miss it?

There’s also the typename property, but I don’t know which values it takes.

Can an Adobe employee that worked on UXP, etc., please provide an answer?

I’d like to know the official reliable way of checking this and, please, if there’s no isGroup() function, introduce it in the future.

group is a layer kind so check the kind of the layer if it’s equal to “group“

1 Like

Ok, maybe that’s the most reliable way, as the documentation lists all kinds of layers here.

I can also check layer.layers, which seems to work, as only groups seem to have layers defined, but is this reliable?

I’m looking for the official way of doing this. It’s probably what you’re suggesting, but why isn’t there a isGroup. It looks like it would be useful

const {constants} = require("photoshop")

const isGroup = (layer) => layer.kind === constants.layerKind.GROUP

Or you can even extend the API yourself

Yes, sure, I know how to write custom functions. I’m wondering why such a function doesn’t exist in the official APIs. It seems that it’s something that would be widely useful.

if(layer.isGroup()) {}

vs

if(layer.kind === constants.layerKind.GROUP) {}

Yes, you could also shorten constants.layerKind.GROUP if you import differently, but yes, I think it would still be useful to have the isGroup()method.

As I mentioned, you can extend the layer API and add this method (or ant other you’d find useful). I personally don’t see an issue, that method doesn’t exist by default, as it’s still a one-liner with a single condition :man_shrugging: