Skip to main content
Known Participant
November 1, 2023
Answered

First line of each text frame

  • November 1, 2023
  • 5 replies
  • 2391 views

Is there a way to specify the number of words or characters in the first line of each text frame that has a given Object style applied?

 

I want to limit the first line to about 3-5 words, for each text frame that has a specific object style applied to it, then every subsequent line to have text as per normal.

This topic has been closed for replies.
Correct answer rob day

Hi @Theo331728749dq2 , A scripting solution might be this—expects a text frame with the applied object style to be selected:

 

 

 

var min = 3
var max = 5
//a selected text frame
var s = app.activeDocument.selection[0];
//random number for forced line break
var rn = Math.floor(Math.random() * (max - min + 1) ) + min;

if (s.constructor.name == "TextFrame") {
    //the selected frame’s object style
    var os = s.appliedObjectStyle;
    var api = app.activeDocument.allPageItems;
    var tf, ii;
    for (var i = 0; i < api.length; i++){
        //get all text frames with the same style
        if (api[i].constructor.name == "TextFrame" && api[i].appliedObjectStyle == os){
            tf = api[i];
            //clear any existing force line breaks
            if (tf.lines[0].characters[-1].contents == SpecialCharacters.FORCED_LINE_BREAK) {
                tf.lines[0].characters[-1].contents = ""
            }
            //insert line break
            var ip = tf.lines[0].words[rn].insertionPoints[0]
            ip.contents = SpecialCharacters.FORCED_LINE_BREAK;
        }
    };   
} 

 

 

 

5 replies

rob day
rob dayCorrect answer
Community Expert
November 5, 2023

Hi @Theo331728749dq2 , A scripting solution might be this—expects a text frame with the applied object style to be selected:

 

 

 

var min = 3
var max = 5
//a selected text frame
var s = app.activeDocument.selection[0];
//random number for forced line break
var rn = Math.floor(Math.random() * (max - min + 1) ) + min;

if (s.constructor.name == "TextFrame") {
    //the selected frame’s object style
    var os = s.appliedObjectStyle;
    var api = app.activeDocument.allPageItems;
    var tf, ii;
    for (var i = 0; i < api.length; i++){
        //get all text frames with the same style
        if (api[i].constructor.name == "TextFrame" && api[i].appliedObjectStyle == os){
            tf = api[i];
            //clear any existing force line breaks
            if (tf.lines[0].characters[-1].contents == SpecialCharacters.FORCED_LINE_BREAK) {
                tf.lines[0].characters[-1].contents = ""
            }
            //insert line break
            var ip = tf.lines[0].words[rn].insertionPoints[0]
            ip.contents = SpecialCharacters.FORCED_LINE_BREAK;
        }
    };   
} 

 

 

 

Robert at ID-Tasker
Brainiac
November 5, 2023

Great script - as always. 

 

But I can bet, that OP want this to work only when paragraph starts in the TextFrame? Not middle lines? 

 

So extra check if Paragraph starts in TextFrame would be needed? 

 

Unless those are unthreaded TextFrames - effect of DataMerge? 

 

rob day
Community Expert
November 5, 2023

Another question is whether @Theo331728749dq2  really wants a random break or is it the " best" break given a short measure?

Lukas Engqvist
Community Expert
November 4, 2023

I guess you can set a nested style or GREP style that makes the 6th word too large to fit in the frame causing an overset. You would have two paragraph styles theobject style setting to "first" and Next style of "first" should be "following" (and followingdoes not have that GREP or Nested style)

Peter Spier
Community Expert
November 4, 2023

Using two frames as I suggested earlier requires only a single paragraph style and no manual manipulation to force a break. The first frame will accept text up to the point where the right inset is met and the remaining text will move to the next frame automatically.

Willi Adelberger
Community Expert
November 4, 2023

I have reconsidered all. Instead of making a line style a separate paragraph with its own paragraph style would be much better. So you can avoid problems with justified text and odd word spacing. Where to separate the first paragraph from the rest is a task of the author. 

Peter Spier
Community Expert
November 4, 2023

What is the alignment of the text?

For right aligned text you could simply use a large first line indent, but for left aligned the only thing that springs to mind is to use two threaded text frames, the first only one line high, and add a hefty right indent to that one-line frame.

Robert at ID-Tasker
Brainiac
November 5, 2023

But then you would need a script to calculate right indent for each case / 1st TF ? 

 

Because OP wants 3-5 words... 

 

Peter Spier
Community Expert
November 5, 2023

OP hasn't set a hard limit. My sense is that they want up to a certain number of characters on the line and that should accomodate three to five "average" length words.

I regard this as basically adding a right indent to the first line (for left aligned text). The right margin would remain either justified or ragged according to the paragraph specs. In justified text the the first line would always be the same length, adjusting the spacing, in ragged text the first line might be shorter on some paragraphs, just as other lines in the paragraph can be short of the margin.

Community Expert
November 1, 2023

One way that might work is to have a GREP or Nested Style built into the Paragraph Style, then have the Object Style trigger the Paragraph style.

So your Paragraph style would have to be Body First Style and then Apply Next Style = Body (all can be triggered in the Object Style)

 

Then in the GREP style for the Body First Style

In the GREP define the amount of words you want - 3 or 5, I can't think of a way to randomise.

And then add no breaks to spaces after these 

 

Maybe a Nested Style would work better?

 

 

Known Participant
November 4, 2023

I havn't been able to get it to work.

Community Expert
November 4, 2023

Can  you give an example of the document you're working on and before and after.
You can attach indesign files to your post.

 

Thanks