Move group on a top of layer structure

I need help how to move group with a layer on a top of layer structure. I tried to select first layer but I get the result as in the picture 1.

      {
        _obj: "select",
        _target: [{
            _ref: "layer",
            _id: app.activeDocument.layers[0]._id
        }],
        _isCommand: true,
        _options: {}
      }

Screenshot 2026-01-13 at 13.25.06

Also, I tried with another approach, it works but in the case that group is already on top I get an alert as on picture 2.

      try{
        await batchPlay([
      {
          _obj: "move",
          _target: [{ _ref: "layer", _enum: "ordinal", _value: "targetEnum" }],
          to: {
            _ref : "layer",
            _enum : "ordinal",
            _value : "front",
          },
          _options: {dialogOptions: "dontDisplay"}
      }],
         { synchronousExecution: true, modalBehavior: "execute" }
    )
      }
      catch(e){}

Screenshot 2026-01-13 at 11.25.14

Try this:

{
    "_obj": "move",
    "_target": [
        {
            "_ref": "layer",
            "_enum": "ordinal",
            "_value": "targetEnum"
        }
    ],
    "to": {
        "_ref": "layer",
        "_index": 6 // This is the place where the layer will be moved
    },
    "adjustment": false,
    "version": 5,
    "layerID": [
        app.activeDocument.activeLayers[0].id
    ],
}

In order to find the index where you want to move the layer, you need to count from bottom to top. So could make a recursive function to get all the layers including layers inside groups, UXP is a mess with indexing layers compared to CEP.

1 Like

@photonic thanks for reply, I gave up on the indexing game :slightly_smiling_face:

I found a solution that works for me, maybe it’s robust and if anyone has a better solution please share yours, I’ll be happy to implement it but for now this gets the job done. On the other side I hope this solution will help someone with similar request.

Basically, firstly create an adjustment layer, move it to the top and then group it.

      await batchPlay(
      [
      // Create curves adjustment layer
      {
              _obj: "make",
              _target: [{ _ref: "adjustmentLayer" }],
              using: {
                _obj: "adjustmentLayer",
                type: {
                  _obj: "curves",
                  presetKind: { _enum: "presetKindType", _value: "presetKindDefault" },
                },
              },
              _options: { dialogOptions: "dontDisplay" },
      },
      // Here comes the part where I change the values ​​of the curve, assign it a name,...
      ],
            { synchronousExecution: true, modalBehavior: "execute" }
      )

      // Move layer on top
      await app.activeDocument.activeLayers[0].moveAbove(app.activeDocument.layers[0]);

      // Group layer
      await batchPlay(
      [
              // Group selected layers
      {
              _obj: "make",
              _target: [{ _ref: "layerSection" }],
              from: { _ref: "layer", _enum: "ordinal", _value: "targetEnum" },
              _options: { dialogOptions: "dontDisplay" },
      }
            // Here comes the part where I change the colour of the group, assign it a name,...
      ],
            { synchronousExecution: true, modalBehavior: "execute" }
      )