Aligning layer in centre

Heya

Using Photoshop, one can align a layer in the centre by selecting the background + the layer to be centred, and pressing the “Align and Distribute” buttons. I cannot find such equivalents in the API.

Am I missing something, or is it not supported?

It is not implemented in DOM yet. You could instead use Alchemist and batchPlay.

Something like this

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

async function actionCommands() {
   const result = await batchPlay(
      [
         {
            _obj: "align",
            _target: [
               {
                  _ref: "layer",
                  _enum: "ordinal",
                  _value: "targetEnum"
               }
            ],
            using: {
               _enum: "alignDistributeSelector",
               _value: "ADSCentersV"
            },
            alignToCanvas: true
         },
         {
            _obj: "align",
            _target: [
               {
                  _ref: "layer",
                  _enum: "ordinal",
                  _value: "targetEnum"
               }
            ],
            using: {
               _enum: "alignDistributeSelector",
               _value: "ADSCentersH"
            },
            alignToCanvas: true
         }
      ],
      {
         modalBehavior: "execute"
      }
   );
}

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

await runModalFunction();

Heya

Thank you sincerely for your fast response, I’ve never heard of Alchemist before, so I’ll take a look at that.

Hey @Jarda

It seems to be working sometimes, but not consistently (no clue why)
So just to mention, I’m creating a script not a plugin.

I set up Alchemist, but I honestlý don’t really know what it is, you got a guide somewhere on what it exactly is (and how to use it)? Maybe I’m blind, but couldn’t find it in their repo.

Here’s a beginner’s guide to using Alchemist:

Thanks Timothy, I’ve watched David’s guide as-well now (excellent quality, same for Jarda’s guide I’m amazed)

What seems to be happening, it runs the batchPlay (seems to be succeeding sometimes?), and then just stops with the script. Added a try-catch around my main and didn’t resolve anything, still insta close.

const app = require("photoshop").app;
const core = require("photoshop").core;
const action = require("photoshop").action;

async function actionCommands() {
    return await action.batchPlay(
        [

            {
                _obj: "select",
                _target: [
                    {
                        _ref: "layer",
                        _name: "Background"
                    }
                ],
                selectionModifier: {
                    _enum: "selectionModifierType",
                    _value: "addToSelection"
                },
            },

            {
                _obj: "align",
                _target: [
                    {
                        _ref: "layer",
                        _enum: "ordinal",
                        _value: "targetEnum"
                    }
                ],
                using: {
                    _enum: "alignDistributeSelector",
                    _value: "ADSCentersH"
                },
                alignToCanvas: false,
                _options: {
                    dialogOptions: "dontDisplay"
                }
            },
            {
                _obj: "align",
                _target: [
                    {
                        _ref: "layer",
                        _enum: "ordinal",
                        _value: "targetEnum"
                    }
                ],
                using: {
                    _enum: "alignDistributeSelector",
                    _value: "ADSCentersV"
                },
                alignToCanvas: false,
                _options: {
                    dialogOptions: "dontDisplay"
                }
            }
        ],
        {
            modalBehavior: "execute"
        }
    );
}

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

Any clues?
I modified the code Jarda gave me to first select the background, and then centre.
Not selecting the background had the same result, would just insta close.

It doesn’t reach the second “runAction” seen below

        await runAction("Image Actions Groot (nobatch)", "Image - INITIAL COPY");
        resize(document.activeLayers[0], bigWidth, bigHeight);
        // await runAction("Image Actions Groot (nobatch)", "Image - CENTRE");
        await align();


        await runAction("Image Actions Thumb (nobatch)", "Image - INITIAL COPY");
        resize(document.activeLayers[0], thumbWidth, thumbHeight);
        await align();

I’m not sure where the mistake was again, been thinking about too much but it’s been resolved.

Thanks a lot for this tool, makes a huge difference with making a plugin!