How to transform a rectangle to 4 custom corner coordinates using batchPlay?

Hi everyone,

I’m new to UXP plugin development and trying to figure out how to transform a rectangular layer using batchPlay.

What I want to do is take a rectangle and move its four corners to custom positions, like this:

// Target corner positions:
Top-left:     (100, 100)
Top-right:    (500, 120)
Bottom-right: (480, 400)
Bottom-left:  (120, 380)

I used Alchemist to record a transform and got this code:

const {executeAsModal} = require("photoshop").core;
const {batchPlay} = require("photoshop").action;

async function actionCommands() {
   const result = await batchPlay([
      {
         _obj: "transform",
         freeTransformCenterState: {_enum: "quadCenterState", _value: "QCSAverage"},
         offset: {_obj: "offset", horizontal: {_unit: "pixelsUnit", _value: 12.99}, vertical: {_unit: "pixelsUnit", _value: 50.39}},
         width: {_unit: "percentUnit", _value: 128.88},
         height: {_unit: "percentUnit", _value: 94.23},
         skew: {_obj: "paint", horizontal: {_unit: "angleUnit", _value: -2.65}, vertical: {_unit: "angleUnit", _value: 0}},
         angle: {_unit: "angleUnit", _value: -6.80},
         using: {_obj: "paint", horizontal: {_unit: "percentUnit", _value: -0.06}, vertical: {_unit: "percentUnit", _value: 0.10}},
         replaceLayer: {_obj: "transform", from: {_ref: "layer", _id: 3}, to: {_ref: "layer", _id: 3}},
         _options: {dialogOptions: "dontDisplay"}
      }
   ], {});
}

async function runModalFunction() {
   await executeAsModal(actionCommands, {"commandName": "Action Commands"});
}

await runModalFunction();

But this only gives me limited control. It’s really difficult to adjust these values to match specific corner points exactly.

Is there a better way to directly set the four corner positions?

Are you wanting to just move the corners in the Transform tool modal state?

The current transform methods in the DOM simply apply the transformation without going through the Transform tool.

1 Like

Can you share a sample code for this?