Site logo


Find hyphenated words

Dave Saunders
There may be some other legitimate line-ending characters I'm overlooking that need to be added to the string. Or you might be able to use grep to simplify that statement.

myStory = app.selection[0].parentStory; 
myLines = myStory.lines.everyItem().contents; 
for (var j = myLines.length - 1; j >= 0; j--) { 
  if ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-/:".indexOf(myLines[j].slice(-1)) != -1) { 
    alert("Line " + j + " of story ends in a soft hyphen.\n\n" + myLines[j]); 

Dirk Becker

story = app.activeDocument.stories[0]; 
lines = story.lines; 

for( i=0; i < lines.length-1; i++ ) { 
	l = lines[i]; 
	w = l.words[-1]; 
	if( l.characters[-1].index==w.characters[0].index+w.characters.length ) 
	$.writeln("line "+i 
	+" l.c.i "+l.characters[-1].index 
	+" w.i "+w.index 
	+" w.c.i "+w.characters[0].index 
	+" w.c.l "+w.characters.length 
	+" w.c.z "+(w.characters[0].index+w.characters.length) 
	$.writeln("line="+i+" word="+w.contents); 

Peter Kahrel
The three methods described here don't take into account hyphenated words like 'cross-eyed'. If you don't want to include these words when they break on the hyphen, you need to test whether the line ends in a hyphen. Something like

l.words[-1].lines.length == 2 && l.characters[-1].contents != '-';

doesn't find 'cross-eyed', even when it breaks across two lines. It finds 'cross-linguistic' when 'linguistic' is broken, but not when the line breaks at 'cross-'.

this will get the hyphen

Line.prototype.getHyphen = function(){
  try{if(this == null || this == undefined){return undefined;}}catch(e){return undefined;}
  var nextThis = this.paragraphs[0].lines.nextItem(this);
  if(nextThis != null){
    if(this.words != null){
      if(this.words.length > 0){
        if(nextThis.words != null){
          if(nextThis.words.length > 0){
            var line0word_1 = this.words[-1];
            var line1word0 = nextThis.words[0];
            if(line0word_1.hyphenation && line1word0.hyphenation){
              if(line0word_1 == line1word0){
                return line0word_1;
  return undefined;

tobias wantzen, Shane Stanley

set theList to {} 
tell application "Adobe InDesign CS2" 
	tell document 1 
		set theWords to object reference of every word of every story where baseline of character 1 is not baseline of character -1 
		repeat with aWord in theWords 
			set firstLine to word -1 of (get line 1 of aWord)
			set secondLine to word 1 of (get line 2 of aWord) 
			set end of theList to firstLine & "-" & secondLine 
		end repeat 
	end tell 
end tell 
return theList

In CS3 and later you find hyphenated words using the GREP expression "\\w+-\\w+"
To find out if they break across lines, check if the first and last characers are on the same line, for example by comparing their baseline values.

//it's hyphenated

Report Page Hyphens script