Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Align layer to x, y position relative to another layer's coordinates using Photoshop Script.

Community Beginner ,
May 23, 2021 May 23, 2021

Copy link to clipboard

Copied

Attempting to create an automated task for jpg batch processing using Actions, but so far was unable to automate alignment of a layer (it's reposition) relative to another layer's coordinates, which is necessary for subsequent actions to be carried out properly.

 

Problem is as follows:

 

As is apparent in the video, there are five layers, names of which are all universal in all files seeing as their naming is done via Actions, though only the top three are of importance to the subject in question, those being, from top to botom: 

 

IsolatedCentered

Isolated

OriginalLayer

 

The task is to input x, y positional values for OriginalLayer that are result of distance between the other two layers and therefore can also be acquired by subtracting x and y coordinates of Layer Isolated from layer IsolatedCentered.

Imagining it would go something along the lines of:

 

#target photoshop

...

OriginalLayer (x) = IsolatedCentered (x) - Isolated (x)

OriginalLayer (y) = IsolatedCentered (y) - Isolated (y)

 

although, in JavaScript syntax of course, however, as the case is currently such that time is in short supply and not substantial enough amount of it can be put towards getting into the subject of coding in JavaScript, but since the task seems it would be only as big as 20 lines of code, if that, hopefully the question will come across the line of sight of someone that has enough Photoshop scripting skill necessary for it, preferably in JavaScript. If inconvenient, seeing as the task is being carried out Windows machine, VBS is an option as well.

 

Thanks in advance.

 

P.S.

 

Reason for turning to scripting in Photoshop is, at least as far as current awareness of capabilities of Photoshop tools, there is no way currently known by which the necessary operation can be accomplished by those means.

Align tools don't work as the center of the layer that is to be moved is already at the center of the canvas, and locking the reference layer disables the tool set, rendering it useless.
Also tried, Auto-Align Layers, however, unlike inputing and y coordinates manually, that one does not align to pixel level accuracy.

 

After a few days of roaming the Photoshop related forums and how-to sites in search of the script, or parts of multiple ones that could be put together, since no suitable code has been found so far, not a small factor in which being that Javascript sklls are currently on beginner level, at best, having just become aware of Photoshop Scripting, and the fact that they can be Macro recorded (as an Action), this currently seems like the only solution left, that is, looking for the answer to the problem by means of asking specifically.

Manual Layer Repositioning Process
TOPICS
Actions and scripting, How to, Windows

Views

921

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Adobe Community Professional , May 23, 2021 May 23, 2021
// get x,y of Isolated layer in px var r = new ActionReference(); r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("bounds")); r.putName(stringIDToTypeID("layer"), "Isolated"); var d = executeActionGet(r).getObjectValue(stringIDToTypeID("bounds")); var x0 = d.getUnitDoubleValue(stringIDToTypeID("left")); var y0 = d.getUnitDoubleValue(stringIDToTypeID("top")); // get x,y of IsolatedCentered layer in px var r = new ActionReference(); r.putProperty(stringIDToTypeID("property"), stringI...

Likes

Translate

Translate
Adobe Community Professional , May 23, 2021 May 23, 2021
A script can retrieve information about layers bound it location over the canvas. Can retrieve the width and height of a document canvas know the aspect ratio of layer and document. A script can use logic to calculate where to move/align a layer to center a layer relative to any pixel in the document canvas,    Action  can have inserted conditional steps,  However the are only a few document and layer condition that can be tested. Actions can not retrieve information  about sizes and locations a...

Likes

Translate

Translate
Adobe Community Professional ,
May 23, 2021 May 23, 2021

Copy link to clipboard

Copied

// get x,y of Isolated layer in px
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("bounds"));
r.putName(stringIDToTypeID("layer"), "Isolated");
var d = executeActionGet(r).getObjectValue(stringIDToTypeID("bounds"));
var x0 = d.getUnitDoubleValue(stringIDToTypeID("left"));
var y0 = d.getUnitDoubleValue(stringIDToTypeID("top"));

// get x,y of IsolatedCentered layer in px
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("bounds"));
r.putName(stringIDToTypeID("layer"), "IsolatedCentered");
var d = executeActionGet(r).getObjectValue(stringIDToTypeID("bounds"));
var x1 = d.getUnitDoubleValue(stringIDToTypeID("left"));
var y1 = d.getUnitDoubleValue(stringIDToTypeID("top"));

// select OriginalLayer because "move" works only for active layer, at least in CS6
var r = new ActionReference();
r.putName(stringIDToTypeID("layer"), "OriginalLayer");
var d = new ActionDescriptor();
d.putReference(stringIDToTypeID("null"), r);
executeAction(stringIDToTypeID("select"), d, DialogModes.NO);

// move OriginalLayer
var d = new ActionDescriptor();
var r = new ActionReference();
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
d.putReference(stringIDToTypeID("null"), r);
var d1 = new ActionDescriptor();
d1.putUnitDouble(stringIDToTypeID("horizontal"), stringIDToTypeID("pixelsUnit"), x1-x0);
d1.putUnitDouble(stringIDToTypeID("vertical"),   stringIDToTypeID("pixelsUnit"), y1-y1);
d.putObject(stringIDToTypeID("to"), stringIDToTypeID("offset"), d1);
executeAction(stringIDToTypeID("move"), d, DialogModes.NO);

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

Perfect - Thank You!

 

Edit: There is a single typo on the very end of the third line from the bottom, where the value that gets subtracted should be y0 instead of  y1

line is:

 

d1.putUnitDouble(stringIDToTypeID("vertical"),   stringIDToTypeID("pixelsUnit"), y1-y1);

 

where without the typo it is:

 

d1.putUnitDouble(stringIDToTypeID("vertical"),   stringIDToTypeID("pixelsUnit"), y1-y0);

 

Didn't get noticed until it went into use, where it was clear when the layer moved on x-axis, but not y.

Still, an extremely small price to pay for the bullseye this code set is relative to what was necessary/asked for, especially when taking into account the time period within which it arrived.

 

Also personally added a line to the top of the script

 

#target photoshop

 

having seen it in every JS photoshop script code set observed so far. Did it reflexively when first creating the .jsx file with this code, as it became a habit when pasting lines of code from various PS how-to places on the net in an attempt to put together enough of it (apparently unsuccessfully) to perform the task in question. Don't know if it accomplishes anything, but didn't seem to hurt.

 

Best regards.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

Targeting Ps is needed when you double clicking / pressing .jsx from outside of application.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

Thanks for the information.

 

Was planning to find out the purpose of that line of code in the near future, but this answer takes care of that need.

 

Regards.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 23, 2021 May 23, 2021

Copy link to clipboard

Copied

A script can retrieve information about layers bound it location over the canvas. Can retrieve the width and height of a document canvas know the aspect ratio of layer and document. A script can use logic to calculate where to move/align a layer to center a layer relative to any pixel in the document canvas,    Action  can have inserted conditional steps,  However the are only a few document and layer condition that can be tested. Actions can not retrieve information  about sizes and locations and use the  information to calculate  resizing and alignment move.  Action are recorder Photoshop steps with recorded setting for the step.  If you have Saved selection in you layers document with known Alpha Channel names and  Layers with known Names. Scripts and Action can load the alpha channels as a selection the select a Layer by name and align the layer relative to the active selection. An action can not do much about size where a script could determine if the layer needs to be resized to fit   within the selected.  Complex automation requires the usw of Photoshop scripting

JJMack

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

Imagining, if insight into photoshop coding was more extensive, and more cohesive thoughts were able to form, they would be worded somewhere, but very closely along those lines as those words describe almost exactly the mental picture that took shape as a consequence of being faced with this problem, that being - Actions, as powerful as they are, still have their limitations, seeing as the Actions tool set is in itself an interface component, to pick on a way to describe it, and is therefore an abstraction level above what can be accomplished with scripting, that is, personally manually coding the solution.

 

In other words - couldnt've said it better myself.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

It's late and I may be missing something here... Isn't this just aligning two object that are the same size that have different offset positions?

 

* Load selection from the layer with the correct position

* Select the layer to move

* Align horizontal to selection

* Align vertical to selection

* Deselect

 

In an action:

 

align2selection.png

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

LATEST

Unfortunately, no.

In fact, that suggestion was among the first processes to be attempted for execution of the task in question, but does not apply, reason for which is, as was soon after making the attempt realized, that the layer that has to be moved relative to the centered layer (IsolatedCentered) is not the one of same size (Isolated), but is in fact the one from which the two mentioned dimensionally identical layers are derived (OriginalLayer) size of which is identical to the size of the document, as it is itself a copy of the Background layer, thereby being already centered, while it's desired position is that of the layer formed by cropping the subject out of it (IsolatedCentered), and calculation of the offset is done relative to the layer containing the same crop of the subject before centering (Isolated).

For that reason, since Auto-Align Layers, the only other built-in tool known of at that moment that was thought of as possibly being of use was the last operation to be attempted, like others, unsuccessfully, before turning to scripting where the solution offered by r-bin, being that it's right on point was no more or less complex than imagined accomplishing exactly what was needed/asked for.

 

Still, as it is, interest in the subject is appreciated.

 

Kind regards.

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines