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

Community Beginner ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

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. 

 

TOPICS
Actions and scripting, Problem or error, Windows

Views

281

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Enthusiast , Apr 26, 2021 Apr 26, 2021
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.

Likes

Translate

Translate
Enthusiast ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

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 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

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. 

s3niRa_1-1619452653269.png

 

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). 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

And in the documentation it says " The height of the bounding box (unit value) for paragraph text. Valid only when kind = TextType.PARAGHRAPHTEXT" And isn't the bounding box the box which limits the visibile text? Like the box I create with the text tool in photoshop before writing my text in it? 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

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-... 


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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

LATEST

Thank you, I will read through it. I might have some questions afterwards I guess, it seems to be a complicated topic... 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines