8

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.

Max Kielland
  • 5,259
  • 7
  • 49
  • 86

1 Answers1

26

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]);
}
Max Kielland
  • 5,259
  • 7
  • 49
  • 86
  • 3
    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! – Kalaschnik Apr 08 '13 at 16:31
  • 3
    @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. – Max Kielland Apr 11 '13 at 13:15
  • Note: don't use as("px"). Photoshop works with unitvalues in a weird way: 100 - UnitValue("5px") Result: -95px. 0 + UnitValue("5px") Result: 5px – Tertium Jan 13 '20 at 13:03
  • In addition to @Tertium 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. – manuman94 Aug 11 '20 at 18:19
  • 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]. – manuman94 Aug 11 '20 at 18:50