Copy link to clipboard
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:
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:
OriginalLayer (x) = IsolatedCentered (x) - Isolated (x)
OriginalLayer (y) = IsolatedCentered (y) - Isolated (y)
Thanks in advance.
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 x and y coordinates manually, that one does not align to pixel level accuracy.
Copy link to clipboard
// 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);
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.
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
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.
Targeting Ps is needed when you double clicking / pressing .jsx from outside of application.
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.
Copy link to clipboard
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
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.
Copy link to clipboard
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
In an action:
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.