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?

  • 2
    Could you clarify the wrapping aspect? My answer only works if the text is on a single line. Would you want text on multiple lines? Example: Line 1 is 45px, Line 2 is 50px, so the total you are looking for is 95px? Or 50px? – JohnB May 28 '13 at 17:45
  • Scott, I'm trying to make a kludgey script to center text vertically in a paragraph box, and this information will factor into determining how many lines there are. JohnB, The answer I'm looking for would be equal to the sum of all those lines. – Grant May 28 '13 at 20:47
  • @Grant, would you be able to talk about this in chat? I have an idea, but it still may not be what you're looking for – JohnB May 28 '13 at 21:16
  • ermm... select both layers an hit the center vertically button on the control bar? You need vertical heights right? Why does your question repeatedly reference width then? – Scott May 28 '13 at 21:27

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.

  • The height and width properties return the measurements of the paragraph bounding box, I think...right? Which would explain the goofy values, but uh I'm not sure how you're getting such tiny bounding box values – JohnB May 28 '13 at 18:04
  • @JohnB: I think you're right, but TextItem appears to be the lowest, most atomic object in the hierarchy. I think the values being returned are a bug (shocker) because they have no basis in reality from what I see. – Philip Regan May 28 '13 at 18:13

For Single Lines of Text:

This is a really rinky-dink way of doing it, but you could rasterize the text layer and then measure it:

var artLayerRef = activeDocument.activeLayer;

var newLayer = artLayerRef.duplicate();
var width = newLayer.bounds[2] - newLayer.bounds[0];


I clone the layer, rasterize it, get the width, then delete it.

Measuring Script

For text that wraps:

Based on the method above, same principle. This is an extremely stupid way of doing it, but I don't see any methods in the scripting reference that would allow you to otherwise easily accomplish this.

Essentially, we're setting the width of the document to the maximum (30,000px) and the same for the text box. If your text is wider than 30,000px then this won't work.

preferences.rulerUnits = Units.PIXELS;
var artLayerRef = activeDocument.activeLayer;
var originalUnit = preferences.rulerUnits;
var docH = activeDocument.height;
var docW = activeDocument.width;

var newLayer = artLayerRef.duplicate();
var textItem = newLayer.textItem;

//It might help if the text box's x coordinate is 0 but shouldn't really matter
activeDocument.crop([0, 0, 30000, docH]);  // 30000px = maximum width
textItem.width = 30000; 

var width = newLayer.bounds[2] - newLayer.bounds[0];

activeDocument.crop([0, 0, docW, docH]);
preferences.rulerUnits = originalUnit;

  • Good idea, though that also assumes the text is by itself on a layer and alters the document like mine. – Philip Regan May 28 '13 at 18:14
  • This would be a good start if there were a way to detect how many wrapped lines of text there are. – Grant May 28 '13 at 18:19
  • @Grant, would you mind fleshing out how wrapped lines should be handled? I think I can come up with an even more rinky-dink way to handle wrapped lines, but it depends on what exactly you're trying to measure – JohnB May 28 '13 at 18:26
  • I'm trying to measure the width the string would be if it weren't wrapped; if it were just point text. – Grant May 28 '13 at 19:17
  • @Grant, I updated my answer with an even sillier method for text wrapping. I'm not clear on what it is you're using this for, but maybe that would work for you. – JohnB May 28 '13 at 20:18

My apologies but your question is lacking detail on what you are trying to accomplish. Based on the fact you wanted to use script may I suggest looking into Imagemagick? Imagemagick is terminal based and you will be allowed to script text options if that is something you are trying to accomplish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.