Photoshop CS6 – Get width of paragraph text in pixels by script

Is there any way to get the width of the text inside a paragraph box? The measurement would not take into account the wrapping. That is, it would equal the sum of each wrapped line.

I’m not asking about the width of the box.

For example, let’s say I have a paragraph text containing the string, “!!!!!”. In Arial at 12pt, that is 23 px. But if the content were, “mmmmm”, the width would be 54px. So the measurement would need to take into account the width of the specific characters in the string.

I can measure that with the ruler tool, but is there a way to get such a measurement in a script?


In short, not without altering the document in some way that isn’t terribly useful. A bit of a brainstorm follows (in Javascript)…

Dimensions of text can only be given if the text is set to a kind of TextItem.PARAGRAPHTEXT, so discounting text wrapping would require extra calculations on your own as the PARAGRAPHTYPE requires wrapping. In terms of something actionable, there is a bit of a drill down to get there…

var doc = activeDocument;
var artLayer = activeDocument.activeLayer;
var textItem = artLayer.textItem;
textItem.kind = TextType.PARAGRAPHTEXT
alert(textItem.width + ", " + textItem.height);

I question the values returned, however, since 6 pt type of the word “HELLO” in Times returned the dimensions of 1.49817590332031 pt width and 0.69983995056152 pt height. That’s horribly wrong since it is ~3 pt by ~19 pt.

The only other way I can think of to get the dimensions would be set the type in a plain box, no wrapping, trim to the top left pixel color, and get your dimensions that way, so…

var doc = activeDocument;
activeDocument.trim (TrimType.TOPLEFT);
alert(activeDocument.height + ", " + activeDocument.width);

…which yields 4.248 pt by 19.368 pt which I trust a lot more. But, still that’s a trim action which assumes that the target text is in a document all by itself.

Source : Link , Question Author : Grant , Answer Author : Philip Regan

Leave a Comment