Transforming the active layer via batchPlay() does nothing, returns no error messages

Before asking this, I checked here and here to get an idea of how to construct things. It seems that, for a layer transformation to work via batchPlay(), I need to have the active layer selected (which I do), and I need to specify the target layer ID in the action descriptor. So I cast the action descriptor as a function return value:

function eventTransformSelection(id, horiz = 0, vert = 0, scale = 100) {
  return {
    "_obj": "transform",
    "_target": [
      {
        _ref: "layer",
        _id: id
      }
    ],
    "freeTransformCenterState": {
      "_enum": "quadCenterState",
      "_value": "QCSAverage"
    },
    "offset": {
      "_obj": "offset",
      "horizontal": {
        "_unit": "pixelsUnit",
        "_value": horiz
      },
      "vertical": {
        "_unit": "pixelsUnit",
        "_value": vert
      }
    },
    "width": {
      "_unit": "percentUnit",
      "_value": scale
    },
    "height": {
      "_unit": "percentUnit",
      "_value": scale
    },
    "replaceLayer": {
      "_obj": "transform",
      "from": {
        "_ref": "layer",
        "_id": id
      },
      "to": {
        "_ref": "layer",
        "_id": id
      }
    }
  };
}

I have a button which should take input values and populate that action descriptor:

document.getElementById("btn-transform-selection").addEventListener('click', async function () {
  let id = app.activeDocument.activeLayers[0].id;
  let batchCommands = [eventTransformSelection(id, 0, 0, 200)]; // (id, xDisp, yDisp, scalePercent)
  async function resize() {
      return await batchPlay(batchCommands, {});
    }
  }
  try {
      return await executeAsModal(resize, { commandName: "Transform Selection" });
    } catch (e) {
      console.error(e);
    }
});

This currently returns no errors. I’ve stuck console logs everywhere on and off to try to figure out where it fails, but I’ve been unable to get this to spit out an error. It should scale the layer by 200%, but nothing happens. It’s gotta be something obvious.

I figured it out. I forgot to deselect before transforming :man_facepalming:
For reference, the event descriptor for a deselection:

const eventDeselect = {
  "_obj": "set",
  "_target": [
    {
      "_ref": "channel",
      "_property": "selection"
    }
  ],
  "to": {
    "_enum": "ordinal",
    "_value": "none"
  },
};

This would feed into the batchPlay function like so:

let batchCommands = [eventDeselect, eventTransformSelection(id, 0, 0, 200)];
1 Like

What I usually do in my plugins now, is:

  1. Get selected layers
  2. Select layers that I need
  3. Do on these layers what I need
  4. Re-select the layers I got on first step