Skip to main content
Retsied
Known Participant
February 21, 2019
Answered

Move layer to specific height (Y) px value (not translate)

  • February 21, 2019
  • 3 replies
  • 607 views

Seems simple enough, but I can't figure it out. How can I move activeLayer to a specific Y-value (in pixels) within the document? I do NOT want to simply translate from current position.

This topic has been closed for replies.
Correct answer r-bin

app.preferences.rulerUnits = Units.PIXELS;

var y = 100; // px

activeDocument.activeLayer.translate(undefined, y - activeDocument.activeLayer.bounds[1].value);

3 replies

Retsied
RetsiedAuthor
Known Participant
February 22, 2019

This seems to work, although I must admit I'm not sure why. Firstly, is the "translate" method not just that, a translation from its current position as opposed to assigning a coordinate location? My canvas is 5400px in height, and when I use the following line you have recommended, it results in a Y-location of roughly 711px. How does this work?

app.activeDocument.activeLayer.translate(0, 400 - activeDocument.activeLayer.bounds[1].value);

Based on the above code line, in my mind this should move the layer from its current position by 400 - 5400 = -5000 px. Then if I were to run it again, it would move another -5000px. This is not the case, however, and does accomplish what I want (a stationary position).

I'm curious as to what's happening here. Regardless though, thanks for the solution!

Stephen Marsh
Community Expert
Community Expert
February 21, 2019

Or perhaps something like​ this?

var savedRuler = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

app.activeDocument.activeLayer.applyOffset(0, 100, OffsetUndefinedAreas.WRAPAROUND);

app.preferences.rulerUnits = savedRuler;

r-binCorrect answer
Legend
February 21, 2019

app.preferences.rulerUnits = Units.PIXELS;

var y = 100; // px

activeDocument.activeLayer.translate(undefined, y - activeDocument.activeLayer.bounds[1].value);