How To Determine If Active Layer Is In A Group?

Use Case:
If the active layer is part of a group, I want to be able to move the layer to the top.

Moving the layer to the top is working but I just canot figure how how to determine if the layer is part of a group.

Can any one help on this one please

There exists a property parent. If the layer is in a group the value is GroupLayer otherwise it’s null.

I initially tried your command but it kept returning this in the console

Screenshot 2022-05-13 at 14.17.18

Then I realised something Kerri wrote on a blog post about it now returns a proxy.

We now have to use Array.from()

Yes, I still used API v 1.
But require("photoshop").app.activeDocument.activeLayers[0] should still work (I assume only one layer is selected and this one you like to check).

I thought I had fixed this but I am getting Layer as the Parent whilst you got GroupLayer

IIRC Layer and GroupLayer have been merged in an update. So it still gives you the information you need (= whether your layer is in a group or not)

Sorry Simon, you have lost me me on this one :slight_smile:

There’s no more LayerGroup, only Layer.
parent property is null if layer is not in a group. Quote from the docs:

The group layer this layer is in, null if the layer is a top layer in the document

1 Like

@Karmalakas ah… I see now having read the docs. Probably need to read this a few times before it sinks in to my 50+ year old brain :slight_smile:

I thought I would simply be able to do something like this but it throws an error if the active layer is no in a group

if (app.activeDocument.activeLayers[0].parent.kind == "group") {
        console.log("Layer is in a group")
    } else {

        console.log("Layer is not in a group")

I don’t think you need to check the kind – just that if parent is anything other than undefined.


if (app.activeDocument.activeLayers[0].parent) {
    console.log("Layer is in a group")
} else {
    console.log("Layer is not in a group")

Having kind here should be superfluous – it would always be group, because parent.kind can only exist for groups.

(Side note)

You’re running into fun JavaScript territory where property access on undefined results in these kinds of funny errors. You could use the ?. operator to manage this:

if (app.activeDocument.activeLayers[0].parent?.kind === "group") /* ... */

But in this case, that’s superfluous, since the kind here would only ever be “group” or undefined. Since that matches the semantics of the parent key itself, it’s just easier to use that.

@kerrishotts Thank you for explaining as to why my example was failing.

So if I have understood you correctly, then using parent.kind was the culpret as it was throwing the error because of the “fun JavaScript territory” as you put it.

O and btw, I am ashamed to say I had to look up what “superfluous” meant :slight_smile: but it’s a nice word. :slight_smile:

1 Like