How can I move some layers into a group layer

If it is a new group layer, I can specify the layers to include when creating a new group.like this:

{
    _obj: 'make',
    _target: [
      {
        _ref: 'layerSection',
      },
    ],
    from: {
      _ref: 'layer',
      _id: fromLayer._id,
    },
    using: {
      _obj: 'layerSection',
      name: groupName,
    },
    _isCommand: true,
    _options: {
      dialogOptions: 'dontDisplay',
    },
  }

or by DOM API document. createLayerGroup({fromLayers: [layer1, layer2], ...otherOption})

But if the group layer already exists, I don’t known how to do this.

I tried using Alchemist and drag a layer into group, and I got this:

 {
      "_obj": "move",
      "_target": [
         {
            "_ref": "layer",
            "_enum": "ordinal",
            "_value": "targetEnum"
         }
      ],
      "to": {
         "_ref": "layer",
         "_index": 10
      },
      "adjustment": false,
      "version": 5,
      "layerID": [
         44
      ],
      "_isCommand": true,
      "_options": {
         "dialogOptions": "dontDisplay"
      }
   }

I guess to._index is the group index that batchPlay need, but, how can I get the index field of the group?

any suggestions?

I assume that’s why you asked about getting the index by name? (Answer here)

Moving the layer to the index is the correct way. If you want the layer to be at the top of the group it’s rather easy: You just move it to the groups index - 1.
However, moving it to the bottom will become a lot more complex. Groups in Photoshop are a bit like HTML-tags, they have a start and an end, with the content in between.

In order to get the index of the group end, you’d have to iterate all layers (starting at the group index) to find the ending. However, groups can also contain other groups, so you need some kind of counter that you increase and decrease for every group start or end. Once the counter is 0 and you find a group end, that’s the end of your original group.

1 Like

This is not a elegant solution but you could always copy the layer and paste it into a new layer where you want it to go. Then delete the old layer and rename the new layer. I do this in one of my programs because it is too much of a pain to deal with the indexes with a bunch of nested groups that are always different each time it runs. Knowing the layer names where it needs to put things makes it easy with copy and paste though.

If it is a new empty group then create the group and a temp layer inside the group. Then select the temp layer and paste the copied layers and they will paste above the temp layer in the group. Then delete the temp layer and the original layers. Yes, it is a clunk work-around but it is simple and it works without hassel.

Cool, I solved it by this way