Put a layer group inside another layer group

What is the correct way to put a layer group inside another layer group?

I’ve tried both

await subGroup.moveInside(mainGroup);

and

await subGroup.move(mainGroup, constants.ElementPlacement.PLACEINSIDE);

They both throw this error:

  TypeError: Cannot read property 'id' of undefined
      at _directRef (uxp://uxp-internal:1)
      at Layer.moveInside (uxp://uxp-internal:1)
      at Layer.move (uxp://uxp-internal:1)
      at createGroups (VM14 index.js:238)
      at async _internalExecuteAsModalWrapper (C:\Program Files\Adobe\Adobe Photoshop 2022\Required\UXP/common.js:54)
      at async _internalCallbackWrapper (C:\Program Files\Adobe\Adobe Photoshop 2022\Required\UXP/common.js:35)

This is using API v2 and the code is being run inside a modal scope

Here is the full code:

async function createGroups(executionContext, descriptor)
{
	try {
		let mainGroup = await app.activeDocument.createLayerGroup({ name: "main group" });
		let subGroup = await app.activeDocument.createLayerGroup({ name: "sub group" });
		await subGroup.move(mainGroup, require('photoshop').constants.ElementPlacement.PLACEINSIDE);
	}
	catch (err) {
		console.log(err);
	}
}
	
async function RunClicked()
{
	await require('photoshop').core.executeAsModal(createGroups, {"commandName": "Creating nested groups"});
}

Edit:
I get the same problem when trying to move a normal layer inside a group layer. The only solution I can see at this stage is to move back to API v1 where .moveBelow() acts as moveInside().

5 Likes

Bump.

Also experiencing this error with move() and moveInside():

Uncaught TypeError: Cannot read properties of undefined (reading 'id')

Snippet:

(layer 0 is a folder layer, layer 1 is a regular layer)

const ps = require("photoshop");
const d = ps.app.activeDocument;
const fold = d.layers[0];
await ps.core.executeAsModal(
    async () => {
        d.layers[1].moveInside(fold)
    },
    { commandName: "test" }
  );

Same here. I was really looking forward to this feature as a means to overcome usage of layer index for moving items. As such I would love to know if there is a work around for this (that doesn’t involve index computations). I hope this is getting some traction :slight_smile:

Can you use ID instead of an index?

My understanding is that you can use ID as a layer target in the ‘move’ batchPlay command, but that still requires you to specify the insertion point of the move as an index. Is that not the case?

You can create new instance of layer using exposed constructor. https://developer.adobe.com/photoshop/uxp/2022/ps_reference/media/prototype/

E.g. const layer = new app.Layer(docID, layerID) or maybe arguments are in oposite order I am not sure now.

Here is what I mean: That’s an example of a ‘move’ in batchPlay:

   {
      _obj: "move",
      _target: [
         { _ref: "layer", _id: layer.id}
      ],
      to: {
         _ref: "layer",
         _index: 8
      },
      adjustment: false,
      _options: {
         dialogOptions: "dontDisplay"
      }
   }

_index” under “to” (i.e. the place you move your layer to, in this example: 8) needs to be an index, does it not?

I think that e.g. _id: 1234 is also possible.

Interesting! If you specify a layer by ID (under “to”), how does it know whether to insert after that layer (as sibling) vs. inserting inside (as a child of) that layer? In any case I’m gonna try that out… :slight_smile: