Mobile app version of vmapp.org
Login or Join
Turnbaugh909

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

@Turnbaugh909

Posted in: #AdobePhotoshop #PhotoshopScripting #Text

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?

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Turnbaugh909

3 Comments

Sorted by latest first Latest Oldest Best

 

@Karen819

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.

10% popularity Vote Up Vote Down


 

@Hamaas979

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();
newLayer.rasterize(RasterizeType.ENTIRELAYER);
var width = newLayer.bounds[2] - newLayer.bounds[0];
newLayer.remove();

alert(width);


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



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;

newLayer.rasterize(RasterizeType.ENTIRELAYER);
var width = newLayer.bounds[2] - newLayer.bounds[0];
newLayer.remove();

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

alert(width);

10% popularity Vote Up Vote Down


 

@Michele215

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme