Skip to main content
selvam214
Inspiring
January 2, 2015
Answered

Auto-hyphen identifier

  • January 2, 2015
  • 1 reply
  • 2071 views

Hi,

If we have any special character to find the auto-hyphen for the end of line?

    if(myFound.lines[0].characters[-1].contents == 'special character for auto-hyphen')

This topic has been closed for replies.
Correct answer Chinnadk

Try this,

var doc = app.activeDocument, 

    _stories = doc.stories; 

for(var i=0;i<_stories.length;i++) 

        var _lines = _stories.lines; 

        for(var j=0;j<_lines.length;j++) 

        { 

                if(_lines.words[-1].lines.length >=2)

                {

                        alert(_lines.words[-1].contents)

                    }

            } 

    }

Regards,

Chinna

1 reply

Chinnadk
ChinnadkCorrect answer
Legend
January 2, 2015

Try this,

var doc = app.activeDocument, 

    _stories = doc.stories; 

for(var i=0;i<_stories.length;i++) 

        var _lines = _stories.lines; 

        for(var j=0;j<_lines.length;j++) 

        { 

                if(_lines.words[-1].lines.length >=2)

                {

                        alert(_lines.words[-1].contents)

                    }

            } 

    }

Regards,

Chinna

selvam214
selvam214Author
Inspiring
January 2, 2015

Hi Chinna,

I truly appreciate for this solution and your help in resolving the problem (both).

Thanks,

Selva

Peter Kahrel
Community Expert
Community Expert
January 3, 2015

This is an interesting problem and makes an appearance in this forum every now and then. It's interesting because the solution seems simple, but it's more complicated than it it seems. Also, finding auto-hyphenated words is one of the better examples of how dramatic speed improvements can be gained by tweaking the code. There's a script that the late Teus de Jong and I wrote that collects and displays all auto-hyphenated words; it's here: http://www.kahrel.plus.com/indesign/get_hyphens.jsx. That script shows a very quick method of collecting divided words.

Chenna's script has some interesting problems (I mention this not as a criticism, but because the problem is illustrative): it breaks on tables; if a hyphenated word is the last word of a paragraph, that word is listed twice; it doesn't ignores footnotes; and because of the repeated calls to the line object, the script is slow. The first problem is fixed by changing line 8 as follows:

if(_lines.words.length > 0 && _lines.words[-1].lines.length >=2)

Lines/paragraphs that consist of a table do not contain any words (just a single character, the table). The second problem is trickier. You don't want words that already contain a hyphen, only auto-hyphenated words. And you don't want words followed by en en-dash, em-dash. So the lines you're interested in are those that do not end in a space, a hyphen, an en- or em-dash, or any paragraph break. See the script linked above for details.

Finally, most scripts can be sped up enormously by placing InDesign's objects in one or more arrays rather than processing the objects as they are. Chenna's script takes about 220 seconds to process a text that contains 1250 lines, which in turn contain about 75 hyphenated words. A modified version processes the same text in 3 seconds:

words = app.documents[0].stories.everyItem().lines.everyItem().words[-1].getElements();

for (i = words.length-1; i >= 0; i--) {

    if (words.lines.length > 1) {

        $.writeln (words.contents);

    }

}

The speed gain is in the first line: the script creates one array consisting of the last word in every line. (This script, too, finds the last hyphenated word of a paragraph twice, and skips footnotes).

Peter