Skip to main content
Participating Frequently
April 26, 2021
Answered

TextItem Bounding Box: width, height and x/y coordinates of top left corner

  • April 26, 2021
  • 2 replies
  • 2541 views

Hello there again, 

 

I want to have the width, height and x/y coordinates of the top left corner of a TextItem object in Pixels. 
I already have this( i traverse through every layer, so I already have an active layer, active document and so on): 

app.preferences.rulerUnits = Units.PIXELS; 
app.preferences.typeUnits = TypeUnits.PIXELS; 
var docRef = activeDocument; 

  //for every layer (i traverse through them) I do: 
  if (layer.kind == LayerKind.TEXT){
  
        var size = layer.textItem.size.value; 
        var textSize = new UnitValue (size, 'px'); 
        var height = layer.textItem.size.value; 
        alert(height); //here I get the same value as the size? 
        var textheight = new UnitValue (layer.textItem.height.value, 'px');
        var xposition = layer.bounds[0].value; 
        var ypostition = layer.bounds[1]value; 


I work with the latest Photoshop Version and this is an .jsx script I scripted in Visual Studio Code with the ExtendedScript Debug. 

 

This topic has been closed for replies.
Correct answer jazz-y

Photoshop is not a text editor, so working with text has a lot of problems.

This topic can help you:

https://community.adobe.com/t5/photoshop/how-to-get-text-transform-box-boundary/m-p/11142576?search-action-id=166996335829&search-result-uid=11142576 


Alternatively, you can create a selection on the text layer or rasterize it to get more precise bounds effortlessly.

2 replies

Inspiring
November 18, 2022

This comes a bit late, but there is fairly easy workaround.

 

TextItem.position gives us the xy of the baseline origin. Starting from there, we can align the layer to the edges of the canvas and use those new xys to calculate top, left, bottom, and right of the text box (including empty space, which using the layer bounds will omit). We can also use this to build an accurate text box width and height, since the built-in values scale incorrectly with resolution.

Legend
April 26, 2021

It doesn't bother you that you are writing the same value to both variables?

 

var size = layer.textItem.size.value; 
var height = layer.textItem.size.value; 

 

size property represents the font size, not the size of text layer

layer.bounds array contains 4 values - these are the top left and right bottom coordinates of the points of the layer bounding rectangle. Just subtract left from right to get the width and top from bottom to get the height.

 

layer.textItem.height

 

textItem.height is a property of the paragraph, not the text layer. Have you tried reading the documentation?

Photoshop JavaScript Reference 

Seni27Author
Participating Frequently
April 26, 2021

Oh, you are right, I corrected it right away, but it still doesn't seem to be right. I still don't get the right values. 
And I neither get the right values using layer bounds. 

 

On the left side you can see the data I get from the script and on the right side the ones from Photoshop info-box (B= width, H = height).

var x = layer.bounds[0].value;
var y = layer.bounds[1].value;
var textlayerwidth = (layer.bounds[2].value - layer.bounds[0].value); 
var textlayerheight = (layer.bounds[3].value - layer.bounds[1].value); 

This is some kind of extract after I corrected it. 
And I need the font size, in this case it gets extracted correctly but in other cases it doesn't (it varies even in the same photoshop document).