9 Replies Latest reply on Sep 23, 2011 6:50 AM by vaasu devi

    Find text from current insertion point - indesign cs3 javascript

    vaasu devi

      Hi all,

       

      I need to find the text from the insertion point in CS3 document through javascipt. Built in indesign has this feature. I want to do this in my script.

       

      Anybody have the idea/solutions for ths...

       

       

      Thanks,

      Vaasu

        • 1. Re: Find text from current insertion point - indesign cs3 javascript
          csm_phil Level 4

          Hi Vaasu,

           

          Please try this JS Code!

           

           

          var myDoc = app.activeDocument;
          app.findGrepPreferences = null;
          app.findGrepPreferences.appliedCharacterStyle = myDoc.characterStyles.item("KT");
          var myResults = myDoc.findGrep();
          for (var i = 0; i < myResults.length; i++)
          {
              app.layoutWindows[0].activePage=myResults[i].words[0].parentTextFrames[0].parent;
              app.select(myResults[i].insertionPoints[0]);
              app.selection[0].texts[0];
              //break;
            }
          alert("Done");
          

           

           

          thx

          csm_phil

          • 2. Re: Find text from current insertion point - indesign cs3 javascript
            Haakenlid Level 3

            I don't understand what you want. Do you want the content of the selected text? app.selection[0].contents

            • 3. Re: Find text from current insertion point - indesign cs3 javascript
              vaasu devi Level 1

              Actually my requirement is trying to findgrep within findgrep. So, first get the result of first findgrep, and then i need to findgrep again, following the result of 1st find result. But through scripting, second find also always start from begining of the document only. Actually i need this second search should start from the end of 1st search result  ie. insertionpoint.

               

              vaasu

              • 4. Re: Find text from current insertion point - indesign cs3 javascript
                csm_phil Level 4

                Hi Vaasu,

                 

                Try this one!

                 

                 

                var myDoc = app.activeDocument;
                app.findGrepPreferences = null;
                app.findGrepPreferences.appliedCharacterStyle = myDoc.characterStyles.item("KT");
                var myResults = myDoc.findGrep();
                for (var i = 0; i < myResults.length; i++)
                {
                    app.findGrepPreferences = null;
                    app.findGrepPreferences.appliedCharacterStyle = myDoc.characterStyles.item("Required Style");
                    var mySearch = myDoc.findGrep();
                    app.select(mySearch[0].insertionPoints[0]);
                    //break;
                   app.findGrepPreferences = null;
                  }
                

                 

                thx

                csm_phil

                • 5. Re: Find text from current insertion point - indesign cs3 javascript
                  Haakenlid Level 3

                  How about doing both searches, and then throw away all results from the second search that comes "before" the first match of the first search, if any.

                   

                  But you have to decide what criteria you use for "before". Is it page number and position on the page?
                  Is is index the story and the location of the match in the story? What about text on master pages? What about anchored text frames?

                   

                  On the other hand if you limit the search to a single story, it becomes trivial. Just use the index of the match.

                  • 6. Re: Find text from current insertion point - indesign cs3 javascript
                    vaasu devi Level 1

                    Thanks csm_phil for your interest... see my below code:

                     

                    if(app.documents.length>0)

                    {

                        myDoc=app.activeDocument;

                        var myFoundchar=new Array();

                     

                        app.findTextPreferences = NothingEnum.nothing;

                        app.changeTextPreferences = NothingEnum.nothing;

                        app.findGrepPreferences=     NothingEnum.nothing;

                        app.findGrepPreferences.fontStyle="Italic"

                       

                        app.findGrepPreferences.findWhat ="[\\l\\u].+?( v\. ).+" //"(.+?(v\.).+)";

                       

                     

                    var myResults = myDoc.findGrep();

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

                    {

                            alert(myResults[i].contents);

                        app.findGrepPreferences=     NothingEnum.nothing;

                        app.findGrepPreferences.findWhat =", \\d{1,}.+?\\d{4}?\\) ";

                     

                        var mySearch = myDoc.findGrep();

                        app.select(mySearch[0].insertionPoints[0]);

                        alert(mySearch[0].contents);

                        app.findGrepPreferences = null;

                      }

                     

                    alert("Done");

                    }

                     

                    here i am trying the fetch the Table of cases, example find results are

                     

                    Sears, Roebuck & Co. v. Camp, 124 N.J. Eq. 403, 407 (E. & A. 1938)

                    Sears, Roebuck & Co. v. Camp, 124 N.J. Eq. 403, 408 (E. & A. 1938)

                    Gramatan Nat’l Bank & Trust Co. v. Backman, 30 N.J. Super. 349 (App. Div. 1954)

                     

                    So, the 2nd search mysearch always giving first line output above.           , 124 N.J. Eq. 403, 407 (E. & A. 1938)

                    If i use the  mySearch[i].contents sometimes it doesn't give the correct result. Since, if gives if other than the case name ends with , and digits. It makes the result totally wrong.

                     

                     

                    Thanks,

                    vaasu

                    • 7. Re: Find text from current insertion point - indesign cs3 javascript
                      Haakenlid Level 3

                      It's very hard to understand exactly what you want to accomplish and what your problem is.

                       

                      I'm very confused by this line:

                       

                      app.findGrepPreferences.findWhat ="[\\l\\u].+?( v\. ).+" //"(.+?(v\.).+)";

                       

                      This gives the following GREP expression: [\l\u].+?( v. ).+

                       

                      It's a valid expression, but is it really what you want to use. It doesn't make much sense to me.

                      • 8. Re: Find text from current insertion point - indesign cs3 javascript
                        vaasu devi Level 1

                        Yes. You are right. it is

                         

                        app.findGrepPreferences.findWhat ="[\\l\\u].+?( v\. ).+";

                         

                        It will get the below text as output.

                         

                        Sears, Roebuck & Co. v. Camp,

                         

                         

                        Given the sample output i need in my previous reply.

                        • 9. Re: Find text from current insertion point - indesign cs3 javascript
                          vaasu devi Level 1

                          app.findGrepPreferences.findWhat ="[\\l\\u].+?( v\. ).+";

                           

                          here i try to get the output based on the value " v. " and the ? before this if for shortest match