5 Replies Latest reply: Apr 24, 2012 6:07 AM by John Hawkinson RSS

    Searching GREP expression through scripts

    Ranjan_123_singh Community Member

      <note id=”law-(\d+)-note-(\d+)” type=”footnote”>

      //-------------------------------------------------------

      Hi,

              Using the above syntax in the code written below, I am trying to search all the note id's from footnotes, only through scripting. But every time I run it I am getting an error "d is undefined" . Please anyone tell me how to do it.

       

      code

      //-----------------

      var myDocument = app.activeDocument;

      var myStory = app.documents.item(0);

      app.findGrepPreferences = null;

      app.changeGrepPreferences = null;

      app.findChangeGrepOptions.includeFootnotes = true;

      app.findChangeGrepOptions.includeHiddenLayers = true;

      app.findChangeGrepOptions.includeLockedLayersForFind = true;

      app.findChangeGrepOptions.includeLockedStoriesForFind=true

      app.findChangeTextOptions.wholeWord = false;

       

      var test2="<note id=\"law-"+"\\"+d+"-note-"+"\\"+d+"\""+" "+"type=\"footnote\">"

      app.findGrepPreferences.findWhat =test2

      app.select(app.activeDocument.textFrames.item(0).findGrep())

       

      app.findGrepPreferences = null;

      app.changeGrepPreferences = null;

      //------------------------------

        • 1. Re: Searching GREP expression through scripts
          John Hawkinson Community Member

          I don't see how this could possibly work.

          Can you please post a screenshot of what you are searching for in InDesign, with Show HIdden Characters enabled?

          But it looks like you're searching for an XML representation, I don't see how that could ever work.

           

          var test2="<note id=\"law-"+"\\"+d+"-note-"+"\\"+d+"\""+" "+"type=\"footnote\">"

          This produces an error because it references the variable "d".

          I don't know what you're trying to do, but you are concatenting several strings to form test2. Let's write it like this:

           

          var test2="<note id=\"law-"+
               "\\"+
               d+
               "-note-"+
               "\\"+
               d+
               "\""+
               " "+
               "type=\"footnote\">"
          

           

          Again, I don't know what you intend, but that clearly references the contents of a variable called 'd', which does not exist.

          I suppose you want something like "\\d+" instead of "\\"+d but that's just not going to work either, for other reasons....

           

          I would probably write the regexp as

           

          var test2=/<note id=”law-(\d+)-note-(\d+)” type=”footnote”>/.source;
          

           

          which should (hopefully) solve your quoting problems...but it still won't work.

          • 2. Re: Searching GREP expression through scripts
            Ranjan_123_singh Community Member

            Hi John,

             

                         Actually I have 30 footnotes. From that footnote I want to search using findGrep, all the "<note id". For example in footnotes we have :

             

            <note id=”law-9780199572984-note-010001” type=”footnote”>

            <note id=”law-9780199572984-note-010002” type=”footnote”>

            <note id=”law-9780199572984-note-010003” type=”footnote”>

             

            I want to search all these 30, and select all in one time. Please tell me how to do this.

            Manually if I press CTRL+F and paste    <note id=”law-(\d+)-note-(\d+)” type=”footnote”>     in Find/Change using GREP it selects all the 30 <note id...978....type="footnote">

            I want to do this using scripting..

             

            I tried to apply the code that you given to me      var test2=/<note id=”law-(\d+)-note-(\d+)” type=”footnote”>/.source;     but its not working

             

            Thanks in advance,

            • 3. Re: Searching GREP expression through scripts
              John Hawkinson Community Member

              Well, if that's the case, my example ought to work.

              THough...I seem to have ended up with curly-quotes somehow. Did you make them straight-quotes? That's probably necessary. i.e.

               

              var test2=/<note id="law-(\d+)-note-(\d+)" type=”footnote”>/.source;

               

              not

               

              var test2=/<note id=”law-(\d+)-note-(\d+)” type=”footnote”>/.source;

               

              anyhow, in my first reply, I asked:

              Can you please post a screenshot of what you are searching for in InDesign, with Show HIdden Characters enabled?

               

              It would be good for you to do that.

              If the above test2 expression does not work, you should try simplifying it until it does work.

              Start with

               

              var test2=/<note id="law->/.source;

               

              and make sure that works, and then continue adding in pieces.

              It might be helpful to print it out with

               

              $.writeln(test2);

               

              so you understand what's going on.

              • 4. Re: Searching GREP expression through scripts
                Ranjan_123_singh Community Member

                Thanks John for the reponse.

                 

                The task is to find <note id=”law-9780199572984-note-010001” type=”footnote”> and need to apply paragraph styling for it.

                 

                I tried manually using "Grep" expression its working fine, but when I am trying to run this through scripting it shows up error.

                 

                Please advice.

                • 5. Re: Searching GREP expression through scripts
                  John Hawkinson Community Member

                  Ranjan, I can't help you if you do not answer my questions.

                   

                   

                  Please reread my responses and answer the questions I asked you, especially the ones in boldface.

                   

                  Check, also, the suggested $.writeln() output, and simplifying the regular expression and cutting it down.

                   

                  Again: Help us, help you. Thank you.