Skip to main content
Green4ever
Inspiring
March 1, 2011
Question

[JS] Finding Double hyphens and page range break

  • March 1, 2011
  • 2 replies
  • 1840 views

Hi Everone,

I have written a small script to find the double hyphenated words, and page range breaks (e.g. 85-86 or 85<endash>86).

I need to improve this script a little more.

//For Finding the double hyphenated words......
#targetengine "session";
var brokenWords = new String("Hyphenated Words\n");
var wordArray = new Array();
var myCounter = 0;
var displayNumber = new Array();
var myFind;
main();
if (myCounter > 0)
{
     displayWindow (wordArray);
     }
else{ alert("No Double-hyphenation found");}

function main(){
     var myDoc = app.activeDocument;
     var stories = myDoc.stories;
     myResetFindChangeGrep ();
     app.findGrepPreferences.findWhat ="(\\w+)[-~=](\\w+)";
     myFind = myDoc.findGrep(true);
     var rec_index = 0;
     var indexArray;
     for(i=0; i<myFind.length; i++)
     {
          var myParent = myFind.parentTextFrames[0];
          var constructorName = myParent.constructor.name;
          var myPage;
          while (myParent.constructor.name != "Page"){
               myParent = myParent.parent;
               }
          myPage = myParent.name;
          isBroken(myFind, myPage);
          }
     myResetFindChangeGrep ();
     }//End of main

//-------------------------------Functions------------------------------
function isBroken(wordObj, pageNum){
     var FirstCharLine = wordObj.characters[0].baseline;
     var lastCharLine = wordObj.characters[-1].baseline;
     var FirstLineNum = wordObj.lines;
     if (FirstLineNum.length>1){
          if (FirstCharLine != lastCharLine){
                    //brokenWords = brokenWords + hypenatedWord + "\n";
                    var myText =wordObj.contents;
                    wordArray.push(myText);
                    displayNumber.push(pageNum);
                    myCounter++;
               }//End of Base line check
          }//End of Line check
     }//End of Function isBroken
//Pallete to display the word list

function displayWindow(hyphenArray){
     var w = new Window ("palette", "Hyphenated words");
     if (hyphenArray instanceof Array)//converting array to srting...
     stringArray = hyphenArray.join ("\r");
     //var list = w.add ("edittext", undefined, stringArray,{multiline: true, scrolling: true});
     var list = w.add ("listbox", undefined, "", {numberOfColumns: 2, showHeaders: true,columnTitles: ["Hyphenated Word", "Page"], columnWidths: [135,50]});
     for (ln=0; ln<hyphenArray.length; ln++){
          with (list.add ("item", hyphenArray[ln])){
               subItems[0].text = displayNumber[ln];
               }
          }

          //list.minimumSize.width = 240;
          list.maximumSize.height = $.screens[0].bottom-200;
     var panel1 = w.add('panel');
     panel1.minimumSize.width = 210;
     var group1 = panel1.add('group');
          group1.orientation ='row';
     var close_ = group1.add ("button", undefined, "Close", {name: "ok"});
          close_.maximumSize.width = 44;
     w.show();
     list.onDoubleClick = function ()
     {
          if (this.selection != null )
          //alert(this.selection.subItems[0].text);
          var myIndex = this.selection.index;
          var selectText = hyphenArray[myIndex];
          var selectPage = this.selection.subItems[0].text;
          selectWord(selectText, selectPage);
          
          }
     close_.onClick = function () {w.close ()}
     }//End of Function displayWindow

function selectWord(text, pageNum){
     var myselText = text; var myPageNum = pageNum;
          for (i=0; i<myFind.length; i++){
               if (myFind.contents == myselText){
               app.select (myFind, SelectionOptions.replaceWith);
               app.activeWindow.zoomPercentage = 200;
               }//End if
          }//end for
     }
function myResetFindChangeGrep(){
     app.findGrepPreferences = NothingEnum.nothing;
     app.changeGrepPreferences = NothingEnum.nothing;
     app.findTextPreferences = NothingEnum.nothing;
     app.changeTextPreferences = NothingEnum.nothing
}

     

I need to display the hyphenated word as it looks in indesign (I mean the breaking of the word). Could any help me on this regard.

Thanks,

Green4ever

This topic has been closed for replies.

2 replies

John Hawkinson
Inspiring
March 4, 2011

I must admit I'm a little confused about what you're trying to do. But since no one else has popped up...

What is a double-hyphenated word? Your script appears to search for any word that has a hyphen or an endash. That seems to be only a single dash to me...so I don't understand the double part.

Which part of your script slows down? The findchange or your for loop or what?

As for displaying the actual word break, you can just iterate over all the characters until you find the change in the baseline and take that as where the word breaks, right? Am I missing something?

Thanks.

Larry G. Schneider
Community Expert
Community Expert
March 4, 2011

John,

In Word authors will quite often use a double hyphen instead of an "M"-dash. At least it happens to me. I don't know it this is what the OP is referring to or not.

Green4ever
Inspiring
March 3, 2011

Hi All,

Did Anyone tried this? I suggest you to test in a smaller document first. I am also working on this to improve this better. Hope Some one will be interested in this.

Thanks,

Green

Green4ever
Inspiring
March 4, 2011

This script is taking quiet long time for larger documents can any one suggest to do this in a faster way?????????..

Thanks,

Green4ever....