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.
Photoshop is not a text editor, so working with text has a lot of problems.
This topic can help you:
Alternatively, you can create a selection on the text layer or rasterize it to get more precise bounds effortlessly.
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?
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.
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).
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?
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:
Alternatively, you can create a selection on the text layer or rasterize it to get more precise bounds effortlessly.
Copy link to clipboard
Copied
Thank you, I will read through it. I might have some questions afterwards I guess, it seems to be a complicated topic...
Copy link to clipboard
Copied
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.