Skip to main content
Participant
May 28, 2009
Question

Is there a script for fixing 'one word on a line at end of paragraph'?

  • May 28, 2009
  • 3 replies
  • 1397 views

Hi i'm tired of scanning through text visually to soft return or create non-breaking spaces for the lonely words at the end of a paragrah. I want to select in the paragraph style to not leave less than 2 words at the end of a paragraph. I've been looking for a script for this and can't find one. I'd like to try and make one but don't know where to start. Unless someone can tell me a different way other than manually doing it to each paragraph in question. I'm in CS2 INDD. Any Suggestions?

Shawnna

This topic has been closed for replies.

3 replies

Participant
May 29, 2009

Hi,

Peter Kahrel, in GREP in InDesign CS3 (p. 37), to "avoid signle-word last lines" suggest this regex : \s(?=[-\w[:punct:]]+$) in Find what. ~S in Change to

L. Tournier

Kasyan Servetsky
Legend
May 29, 2009

But CS2 doesn't have GREP search.

Participant
May 29, 2009

Sorry I don't take care about it

Laurent

Le 29 mai 09 à 16:41, Kasyan Servetsky <forums@adobe.com> a écrit :

But CS2 doesn't have GREP search.

>


Thanks everyone, I'm going to try the first one...the script. Wish me luck. I’ll let you know how it goes, maybe someday we’ll get CS3 here. :-)

Shawnna

Inspiring
May 29, 2009

One of the difficulties of fully implementing this by script is that judgment is required. Sometimes, the best result is a single word at the end of a paragraph.

Back in CS days, I wrote about an approach to this, here:

http://www.pdsassoc.com/index.php?Nav=tipssub&Ban=DeruntingParagraphs&Info=tipsCS/DeruntingParagraphs/index.html

I see you're using CS2. That rules out an approach that I was thinking might work involving GREP styles in CS4. Set up a style that looks for at least some fairly large number of characters at the start of a paragraph and then apply No Break to the last n characters, where n is say 15.

Consider: A paragraph that ends with

is it.

Another that ends with

serendipity.

Which of these needs attention? Perhaps neither!

Dave

Kasyan Servetsky
Legend
May 29, 2009

I assume that one text frame is selected. I am on CS3 and it is not properly tested.

var myDoc = app.activeDocument;
var pars = myDoc.selection[0].paragraphs;
for (i = 0; i < pars.length; i++) {
    var par = pars;
    var lastLine = par.lines[-1];
    if (lastLine.words.length < 2) {
        var aWord = par.words[-2];
        var aSpace =  aWord.characters.previousItem(aWord.characters[0]);
        aSpace.contents = "\n";
    }
}

alert("Done");

Alternatively, to replace spaces with nonbreaking spaces you can use the following script:

var myDoc = app.activeDocument;
var pars = myDoc.selection[0].paragraphs;
for (i = 0; i < pars.length; i++) {
    var par = pars;
    var lastLine = par.lines[-1];
    if (lastLine.words.length < 2) {
        par.words[-2].characters.previousItem(par.words[-2].characters[0]).contents = SpecialCharacters.NONBREAKING_SPACE;
        par.words[-1].characters.previousItem(par.words[-1].characters[0]).contents = SpecialCharacters.NONBREAKING_SPACE;
    }
}

alert("Done");

But hyphenate parameter for the paragraphs should be off in this case.