Mobile app version of vmapp.org
Login or Join
Speyer780

: Photoshop: Get x/y origin of paragraph text box via Javascript Is there any way to get the x/y position of the origin of a paragraph text box via Javascript, excluding use of the Photoshop

@Speyer780

Posted in: #PhotoshopScripting #Text

Is there any way to get the x/y position of the origin of a paragraph text box via Javascript, excluding use of the Photoshop UI?

This image shows my predicament; what I need vs what the Photoshop Javascript is giving me:


The .bounds property gives the coordinates of the actual text content, as shown in the image by the guides and the green circles. I got this by running the simple script alert(app.activeDocument.activeLayer.bounds);, which gave these measurements:

What I would expect to be stored in .bounds, and what I need, is the top-left and bottom-right coordinates of the paragraph box. Does anybody have any good way of getting this?

TextItem.width and TextItem.height are getting close, but they give no indication of where on the canvas the text item actually is.

Right now I'm having to find the center of the bounds coordinates and extrapolate from there, but that is not precise enough because it varies 1 or 2 pixels depending on the characters in the text field.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Speyer780

2 Comments

Sorted by latest first Latest Oldest Best

 

@Si6392903

What I'm saying is, that this upper solution is not fully correct.
Because:
textItem.position does not return the correct top upper left position. From what I have found out you need to add kind of an offset at least to "y". In my tests this offset has always been 3px. Thus the bounds are:

[YourTextLayer.textItem.position[0],
YourTextLayer.textItem.position[1]-3, YourTextLayer.textItem.position[0]+YourTextLayer.textItem.width.as('px'),
YourTextLayer.textItem.position[1]+YourTextLayer.textItem.height.as('px')]

But also this turns out not to be the last meaningful solution because it has issues in some cases. So what you need is either a better Photoshop position attribute or an own solution, which I would highly refer. What I did here to compensate for this crazy offset thing is:


Store the original paragraph box size
Change the text to somthing from which you know it's in the top-right corner of the paragraph box (in my case this was an "l" (Arial Bold, 6pt, Left-justified, no antialiasing)
Rasterize the layer
Get the top left position of your rasterized "l"
Add the layer size to the top left position
Figure out that this is closer to the proposed solution (not perfect, but a lot closer) the relative positional error in X is +-0.5 in Y it's around +- 1.25.

10% popularity Vote Up Vote Down


 

@Nimeshi706

What you'll want to use is:
textItem.position, that will return the coordinates of the top-left most point of the bounding box you're looking for.

Example code of textItem.position:

alert(app.activeDocument.activeLayer.textItem.position);

Then if you want the bottom-right point, simple add the height and width of the box to that coordinate.

Something like this:

var right = textItem.position[0] + textItem.width;
var bottom = textItem.position[1] + textItem.height;


position[0] will return the x coordinate and position[1] will return the y coordinate.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme