Photoshop scripting: Move an image to position x,y
Asked Answered
K

1

11

I have an active ArtLayer referenced by the variable NewLayer that I want to move to the absolute position x,y in the canvas.

I have been googling for a couple of hours now without any results. Can some one please give an example?

// Thanks.

Kaleena answered 21/8, 2012 at 22:32 Comment(0)
K
29

After some more API reading and searching I came to the conclusion that it is only possible to move a layer with delta move.

I wrote this little function to position a layer at an absolute position. Hope this is helpful to the next reader with the same question...

//******************************************
// MOVE LAYER TO
// Author: Max Kielland
//
// Moves layer fLayer to the absolute
// position fX,fY. The unit of fX and fY are
// the same as the ruler setting. 

function MoveLayerTo(fLayer,fX,fY) {

  var Position = fLayer.bounds;
  Position[0] = fX - Position[0];
  Position[1] = fY - Position[1];

  fLayer.translate(-Position[0],-Position[1]);
}
Kaleena answered 21/8, 2012 at 23:27 Comment(6)
To implement that open a text file, paste the code in, save it as .jsx. Put that File in your Photoshop Scripts Folder: (C:\Program Files\Adobe\Adobe Photoshop CS6 (64 Bit)\Presets\Scripts) Restart Photoshop, under File --> Scripts its there!Wilcox
@Steve Have you tried this yourself? This is a function that needs 3 parameters and can't be executed as a single script directly from PS. You need to copy the function to your own script and call it with valid parameters.Kaleena
Note: don't use as("px"). Photoshop works with unitvalues in a weird way: 100 - UnitValue("5px") Result: -95px. 0 + UnitValue("5px") Result: 5pxReplay
In addition to @Replay comment, to get the value of a UnitValue you can use the "value" attribute. For example, for a ArtLayer object you can use layer.bounds[0] to know the X coordinate of its left upper corner, but it will be a "UnitValue", so to obtain the number value without the unit (px, cm) use layer.bounds[0].value.Ethel
A little disclaimer of the @MaxKielland answer: the translation will be done using the upper left corner as "anchor point", so the fX and fY values will be the left upper corner target point, not the layer center. If you want fX and fY to be the target center of the layer then calculate the layer width and height, divide them by 2 and add them to Position[0] and Position[1].Ethel
Thanks for this function! I must be confused about it. Here is my question: #77789263 Thanks.Eachelle

© 2022 - 2024 — McMap. All rights reserved.