Unable to move layer into group using ElementPlacement.PLACEINSIDE

I created a group layer, which is layer[0], (king=group) and I am trying to run this code:

const constants = require('photoshop').constants;
var newGroup = await refDoc.createLayerGroup({ name: "mockUp"});
await app.activeDocument.layers[layerIndexToMove].move(newGroup , constants.ElementPlacement.PLACEINSIDE);

Stepping into with the debugger i see an error in common.js:
“TypeError: Cannot read properties of undefined (reading ‘id’)”.

What am I doing wrong?

Looks like error does not relate to any of those 3 lines of code. Also what is refDoc? Did you check what you get as a newGroup? What is layerIndexToMove? Could it be you are trying to move group into itself? After you create a group, indexes might change. Way too little info provided

The error is shows when I step into:

await app.activeDocument.layers[layerIndexToMove].move(newGroup , constants.ElementPlacement.PLACEINSIDE);

refDoc=app.activeDocument
newGroup.kind=group
Yes, I know the layer index changes after adding a group and I am not moving a group into itself.

Well… Error fires while trying to read id, but there’s nothing like it in your code, so it’s probably somewhere deeper. Error should say file and line where it occurs. Debug all your variables. What do you get for app.activeDocument.layers[layerIndexToMove]?

O get a layer object:
Layer {_id: 89, _docId: 506}

And it’s definitely not the same as newGroup? And you didn’t answer where exactly error originates and what’s the code there.

It is not the group layer.
As I wrote, teh errors occurred when I stepped into the move method of the Document, here:


Check error backtrace

how do you “Check error backtrace” in the debugger?

Next to error there should be an arrow on the left to expand error info

ha?
I only see the error when I hoover over the “error”.

Check it before converting to string. You should get more info

I think there is something wrong with this command or I am missing something.
The errors I get are these:

TypeError: Cannot read properties of undefined (reading ‘id’)
at _directRef (uxp://uxp-internal:1)
at Layer.moveInside (uxp://uxp-internal:1)
at Layer.move (uxp://uxp-internal:1)
at createMockup (VM16 app.js:294)
at async _internalExecuteAsModalWrapper (VM10 common.js:59)
at async _internalCallbackWrapper (VM10 common.js:40)

However, I tried a very simply scenario. Can you check if this is working for you?:

    const constants = require('photoshop').constants;
    newLayer = await app.activeDocument.createLayer({ name: "testLayer"});
    newGroup = await app.activeDocument.createLayerGroup({ name: "testGroup"});
    await newLayer.move(newGroup, constants.ElementPlacement.PLACEINSIDE);

I had a try and got the same error, changing it to “placeBefore” worked fine though. As the docs are written it does look like a bug?
Here’s my code and log

const { app, core, constants } = require("photoshop");

    await core.executeAsModal(async () => {
      newLayer = await app.activeDocument.createLayer({ name: "testLayer" });
      newGroup = await app.activeDocument.createLayerGroup({
        name: "testGroup",
      });

      console.log("newLayer", newLayer);
      console.log("newGroup", newGroup);

      await newLayer.move(newGroup, constants.ElementPlacement.PLACEINSIDE);
    });

While PLACEBEFORE works, it placed the layer before but not inside…

Which is what it’s supposed to do…

If you create group first and then layer, it works fine, but only if your active layer (when running script) was the very top one or you had no layers selected at all. But if active layer is other that the top-most, then you’re screwed

1 Like

Just tried lots of different variations of selecting layer before move, selecting group before move, selecting none before move… Then in any case there’s this error. It seems like newLayer should be selected, but it still only works if script is ran when nothing or top-most selected.

I’d stick with batchPlay TBH

I found this
How come it is not shown here ???

First link is for older versions, but not sure now which ones, because it’s a mess :slight_smile: I think it’s for earlier than Ps v23.0 and manifest v5. Probably would still work with manifest v4. As I said, noting versions in the docs is something Adobe doesn’t really bother about :frowning: