Skip to main content
monica_L6890535
Participating Frequently
October 19, 2017
Answered

Can a script determine number of lines in a paragraph?

  • October 19, 2017
  • 2 replies
  • 1900 views

I found the exact same question in the InDesign scripting, but there is no such question in the photoshop scripting.

Generally, I have a textitem that I created in the script. I set up the length is 200px cause I know it would be the maximum. But generally I don't know how many lines are there. I need to know the total number of lines is because I will add extra textitem. By determine the height of my words, I would not overlay other textitems.

This is what I have so far.

if (app.activeDocument.activeLayer.kind == LayerKind.TEXT){

      var textItemRef = app.activeDocument.activeLayer.textItem;

      var content = textItemRef.contents;    //<--- the contents is already set up.

      textItemRef.contents = content.substring(1);

      //3. fix it to the paragraph

      textItemRef.kind = TextType.PARAGRAPHTEXT;

      textItemRef.width = UnitValue(150,"px");

      textItemRef.height = UnitValue(200,"px");

      var just = Justification.LEFT;

      activeDocument.activeLayer.textItem.justification = just;

      //4. fixed to the correct location

      textItemRef.position = new Array(pos[0], pos[1] );

     //5. should return lines number

     return textItemRef.lines??????

Thanks in advance!

This topic has been closed for replies.
Correct answer SuperMerlin

You need to have a text layer selected for this example...

#target photoshop;

if(app.activeDocument.activeLayer.kind == LayerKind.TEXT){

var aT = app.activeDocument.activeLayer.textItem; 

if (aT.kind == TextType.PARAGRAPHTEXT) {

aT.kind = TextType.POINTTEXT;

var tArray = aT.contents.split("\r");

var line = (tArray.length == 1) ? " line" : " lines";

executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO );

alert (tArray.length+line);

}else{

var tArray = aT.contents.split("\r");

var line = (tArray.length == 1) ? " line" : " lines";

alert (tArray.length+line);

    }

};

2 replies

Legend
October 21, 2017

monica.L  написал(а)

By determine the height of my words, I would not overlay other textitems.

May be more useful is to determine hieight of total text in such way

text_height = app.activeDocument.activeLayer.bounds[3] - app.activeDocument.activeLayer.bounds[1];

?

Jarda Bereza
Inspiring
October 20, 2017

You could convert box text into non-box text. This will convert end of lines into break line characters and you can count these characters.

This works with entire layer. If you want only specific part of text layer... it will be harder.

monica_L6890535
Participating Frequently
October 20, 2017

I didn't know my text is called "box text". Do you know how to convert it into "non-box text". Sorry, I'm really new to this. T_T

SuperMerlin
SuperMerlinCorrect answer
Inspiring
October 20, 2017

You need to have a text layer selected for this example...

#target photoshop;

if(app.activeDocument.activeLayer.kind == LayerKind.TEXT){

var aT = app.activeDocument.activeLayer.textItem; 

if (aT.kind == TextType.PARAGRAPHTEXT) {

aT.kind = TextType.POINTTEXT;

var tArray = aT.contents.split("\r");

var line = (tArray.length == 1) ? " line" : " lines";

executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO );

alert (tArray.length+line);

}else{

var tArray = aT.contents.split("\r");

var line = (tArray.length == 1) ? " line" : " lines";

alert (tArray.length+line);

    }

};