11 Replies Latest reply on Mar 8, 2011 2:30 AM by Green4ever

    [JS] Finding Double hyphens and page range break

    Green4ever Level 3

      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[i].parentTextFrames[0];
                var constructorName = myParent.constructor.name;
                var myPage;
                while (myParent.constructor.name != "Page"){
                     myParent = myParent.parent;
                     }
                myPage = myParent.name;
                isBroken(myFind[i], 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[i].contents == myselText){
                     app.select (myFind[i], 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

        • 1. Re: [JS] Finding Double hyphens and page range break
          Green4ever Level 3

          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

          • 2. Re: [JS] Finding Double hyphens and page range break
            Green4ever Level 3

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

             

            Thanks,

            Green4ever....

            • 3. Re: [JS] Finding Double hyphens and page range break
              John Hawkinson Level 5

              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.

              • 4. Re: [JS] Finding Double hyphens and page range break
                Larry G. Schneider Adobe Community Professional & MVP

                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.

                • 5. Re: [JS] Finding Double hyphens and page range break
                  Green4ever Level 3

                  Hi,

                   

                  Yes i'm little worried because no one has replied... Great thanks for your reply first....

                   

                  1. First I'm searching the words with hard hyphen (eg. cross-section, co-worker, 1stword<endash>2ndword...) and page ranges (eg. 133<endash>142).
                  2. After searching these words I'll check the linecount and baseline of every Item.
                  3. If the word "cross-section" is broken in to "cross-sec~tion" ("~" means auto hyphenated by indesign), In my workflow this is considered as bad break. I'll collect these kind of words and displaying in the palette.
                  4. Double clicking the word in the pallete will select the word in the indesign document, with custom zoom size.

                   

                  Hope you understand what I'm trying to do.

                   

                  For smaller document my script will work better. For longer document the for loop takes little more time. It is taking about 40 sec to 1 minute for 70 page text document. Or this is actual time for this action? I don't know exactly.

                   

                  Thanks again....

                  • 6. Re: [JS] Finding Double hyphens and page range break
                    John Hawkinson Level 5

                    Green4ever, I think you need to reset your expectations for this forum.

                     

                    Having no one reply within a day does not mean you should worry.

                     

                    Thanks for clarifying your intent. If your script is running slowly and you're not sure why, you should measure how long the parts of your scripts take. You can call new Date() to get the times and subtract them, or you can use $.hiresTimer if you need to measure really fine-grained events.

                     

                    Anyhow, you might take note of Harbs' FindHyphenation script. He uses:

                     

                    if(text.words[i].lines.length>1)
                    

                     

                    Which is probably a bit faster than what you're doing.

                    • 7. Re: [JS] Finding Double hyphens and page range break
                      Green4ever Level 3

                      Hi John Hawkinson,

                       

                      I have written a new script to do the same action and now it is giving a better performance.

                      I already gone through several script for finding hyphenations. Those scripts are really good, I need some customization from all of that. And as you suggest I'd like to display the time taken. Can you give me a example code for this. I am also trying at my end.

                       

                      #targetengine "session";
                      var myDoc = app.activeDocument;
                      var stories = myDoc.stories.everyItem ().getElements ();
                      var myFind = new Array(); var myCounter = 0;
                      //-----------------------------For display---------------
                      var w = new Window ("palette", "Hyphenated words");
                      var list = w.add ("listbox", undefined, "", {numberOfColumns: 1, showHeaders: true,columnTitles: ["Hyphenated Word"], columnWidths: [185]});
                           list.maximumSize.height = $.screens[0].bottom-200;
                      var myStory_id = new Array(); var myitem_id = new Array(); 
                      //------------------------------------------------     
                      main();
                      displayWindow();
                      if (myCounter > 0){
                      w.show();
                      }
                      else { alert("Double Hypehnation Not Found"); }
                      
                      function main(){
                           var lineCount;
                           myResetFindChangeGrep ();
                           app.findGrepPreferences.findWhat = "(\\w+)([-~=])(\\w+)";
                           var myIndex = new Array();
                           for (i=0; i<stories.length; i++){
                                myFind[i] = stories[i].findGrep(true);
                                for (j=0; j<myFind[i].length; j++){
                                     lineCount = myFind[i][j].lines.length;
                                     if (lineCount>1){                    
                                          var story_id = i; var myFind_id = j;
                                          var myText = myFind[i][j].contents;
                                          addItem(myFind[i][j], i, j);
                                          myCounter++;
                                          }//End if
                                     }//End of Find Length
                                }//End Story length
                           //alert(myIndex.length);
                           }
                      
                      //------------------------Function Declarations--------------
                      
                      function displayWindow(){
                           //window declared as global variable here 
                           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"});
                           group1.add("statictext", undefined, "\u00A9 2011");
                                close_.maximumSize.width = 44;
                           list.onDoubleClick = function ()
                                { 
                                     if (this.selection != null ) {
                                     var myIndex = this.selection.index;
                                     var storyNum = myStory_id[myIndex];
                                     var findNum = myitem_id[myIndex];
                                     var selectText = myFind[storyNum][findNum];
                                     app.select (selectText, SelectionOptions.replaceWith);
                                     app.activeWindow.zoomPercentage = 200;
                                     }
                                } 
                           
                           close_.onClick = function () {w.close ()}
                           }
                      function addItem(myItem, sto_id, item_id){
                           list.add ("item", myItem.contents);
                           myStory_id .push(sto_id);
                           myitem_id .push(item_id);
                           }
                      function myResetFindChangeGrep(){
                           app.findGrepPreferences = NothingEnum.nothing;
                           app.changeGrepPreferences = NothingEnum.nothing;
                           app.findTextPreferences = NothingEnum.nothing;
                           app.changeTextPreferences = NothingEnum.nothing;
                      }
                      

                       

                      Thanks for your time...

                       

                      Green4ever...

                      • 8. Re: [JS] Finding Double hyphens and page range break
                        John Hawkinson Level 5

                        Green4ever wrote:

                        And as you suggest I'd like to display the time taken. Can you give me a example code for this.

                         

                        Sure. I just posted an example over in ESTK/INDESIGN XML Parsing limit ?

                         

                        var times = [];
                        
                        
                        times.push(new Date().valueOf());
                        // do long thing
                        times.push(new Date().valueOf());
                        
                        $.writeln("Time was "+(times[1]-times[0]));
                        

                         

                        Of course, using an array is silly for just 2 data points. But it can be helpful if you benchmark a lot of things -- which you should.

                        • 9. Re: [JS] Finding Double hyphens and page range break
                          Green4ever Level 3

                          Hi John Hawkinson,

                           

                          When I use this new Date().valueOf();

                          I am getting the value times[0] = 1299579169738

                           

                          I refered with the object browser and it says like this...

                          Date (year: number , month: number , day: number , hours: number , min: number , sec: number , ms: number )

                           

                          Can you give a little brief about this....

                          Thanks...

                          • 10. Re: [JS] Finding Double hyphens and page range break
                            John Hawkinson Level 5

                            The valueOf() method returns a value in milliseconds. That makes it very easy to subtract two of them. Then divide by 1000 to get seconds.   

                            • 11. Re: [JS] Finding Double hyphens and page range break
                              Green4ever Level 3
                              The valueOf() method returns a value in milliseconds. That makes it very
                               easy to subtract two of them. Then divide by 1000 to get seconds.    

                               

                              Thanks for clarifying... I got it... I have seen some examples also...