10 Replies Latest reply on Feb 16, 2009 1:07 AM by Peter Kahrel

    Using Grep to find text pattern, get found text, and get page number found on

      Hi there,

      I have been successful in finding text using grep and returning the text, but need to identify what page the text is on.

      This example finds an email address. So far I have:

      Set myInDesign = CreateObject("InDesign.Application.CS3")

      Rem Clear the find/change preferences.
      myInDesign.FindGrepPreferences = idNothingEnum.idNothing
      myInDesign.ChangeGrepPreferences = idNothingEnum.idNothing

      Rem Set the find options.
      myInDesign.FindChangeGrepOptions.IncludeFootnotes = False
      myInDesign.FindChangeGrepOptions.IncludeHiddenLayers = False
      myInDesign.FindChangeGrepOptions.IncludeLockedLayersForFind = False
      myInDesign.FindChangeGrepOptions.IncludeLockedStoriesForFind = False
      myInDesign.FindChangeGrepOptions.IncludeMasterPages = False
      Rem Regular expression for finding an email address.
      myInDesign.FindGrepPreferences.FindWhat = "(?i)[A-Z]*?@[A-Z]*?[.]..."

      Set t = myInDesign.FindGrep

      For i = 1 to t.Count
      Debug.Print t.Item(i).Contents
      ' How do I get the page number that the text was found on?
      Next

      Can anyone help with this?

      Thanks,
      Ric
        • 1. Re: Using Grep to find text pattern, get found text, and get page number found on
          Level 1
          Ric,

          If you can read JavaScript, this has been answered quite recently here:

          Peter Kahrel, "Extracting info from Links panel" #7, 10 Feb 2009 12:16 pm

          If you're just concerned with text, the simpler version of the findWhere function (in message 7) will do the job for you.

          Dave
          • 2. Re: Using Grep to find text pattern, get found text, and get page number found on
            Level 1
            Hi Dave,

            Thank you for the reference. I am trying to work with this code.

            In my example above, would theObj be:

            t.Item(i)

            I translated the javascript to vb and that doesn't seem to be working.

            When I pass it t.item(i) I get an error:

            "Object doesn't support this property or method".

            I must be passing the function the wrong object.

            Function findPage(theObj)

            If (theObj.hasOwnProperty("baseline")) Then
            theObj = theObj.ParentTextFrames(0)
            End If

            While theObj <> Null And theObj <> Page
            Set t = theObj.constructor
            Select Case t
            Case Null
            Case Character: theObj = theObj.ParentTextFrames(0)
            Case Cell: theObj = theObj.InsertionPoints(0).ParentTextFrames(0)
            Case Application
            End Select

            findPage = theObj.Parent
            Wend

            End Function

            Any ideas?

            thanks,
            Ric
            • 3. Re: Using Grep to find text pattern, get found text, and get page number found on
              Level 1
              Ah, hasOwnProperty is a JavaScript only method.

              Since you know that you're passing text, you could just skip that -- eliminate the test and go straight to the parent text frame.

              Dave
              • 4. Re: Using Grep to find text pattern, get found text, and get page number found on
                Level 1
                Hi Dave,<br /><br />OK, so I tried all of these variations and got various errors:<br /><br />t.Item(1).InsertionPoints(0).ParentTextFrames(0)<br />t.Item(1).ParentTextFrames(0)<br />t.InsertionPoints(0).ParentTextFrames(0).item(1)<br />t.ParentTextFrames(0).item(1)<br /><br />I would think that one of the first two would work since they are both  <br />text and the object browser shows ParentTextFrame as a property of text.<br /><br />What am I doing wrong.<br /><br />Thanks,<br />Ric<br /><br />Quoting Dave Saunders <member@adobeforums.com>:<br /><br />A new message was posted by Dave Saunders in<br /><br />INDESIGN SCRIPTING --<br />   Using Grep to find text pattern, get found text, and get page<br />number found on<br /><br />Ah, hasOwnProperty is a JavaScript only method.<br /><br />Since you know that you're passing text, you could just skip that --<br />eliminate the test and go straight to the parent text frame.<br /><br />Dave<br /><br />-------------------------<br />View/reply at Using Grep to find text pattern, get found text, and<br />get page number found on[1]<br />Replies by email are OK.<br />Use the unsubscribe[2] form to cancel your email subscription.<br /><br /><br /><br />Links:<br />------<br />[1] http://www.adobeforums.com/webx?13@@.59b7e2c7/2<br />[2] http://www.adobeforums.com/webx?280@@.59b7e2c7!folder=.eea52bc
                • 5. Re: Using Grep to find text pattern, get found text, and get page number found on
                  Level 1
                  Is the found text overset? That will cause the parent text frames array to be of zero length.

                  Dave
                  • 6. Re: Using Grep to find text pattern, get found text, and get page number found on
                    robin521
                    i think i have same pro with you, <br /><br />function myReadPMTags(myStory){<br />var myName, myString, myStyle, myStyleName;<br />var myDocument = app.documents.item(0);<br />//Reset the findGrepPreferences to ensure that previous settings<br />//do not affect the search.<br />app.findGrepPreferences = NothingEnum.nothing;<br />app.changeGrepPreferences = NothingEnum.nothing;<br />//Find the tags (since this is a JavaScript string,<br />//the backslashes must be escaped).<br />app.findGrepPreferences.findWhat = "[^A-Za-z0-9/.-]+";<br />var myFoundItems = myStory.findGrep();<br />if(myFoundItems.length != 0){<br />var myFoundTags = new Array;<br />for(var myCounter = 0; myCounter<myFoundItems.length; myCounter++){<br />myFoundTags.push(myFoundItems[myCounter].contents);<br />}<br />myFoundTags = myRemoveDuplicates(myFoundTags);<br />//At this point, we have a list of tags to search for.<br />for(myCounter = 0; myCounter < myFoundTags.length; myCounter++){<br />myString = myFoundTags[myCounter];<br />//Find the tag using findWhat.<br />app.findTextPreferences.findWhat = myString;<br />//Extract the style name from the tag.<br />myStyleName = myString.substring(1, myString.length-1);<br />//Create the style if it does not already exist.<br />try{<br />myStyle = myDocument.paragraphStyles.item(myStyleName);<br />myName = myStyle.name;<br />}<br />catch (myError){<br />myStyle = myDocument.paragraphStyles.add({name:myStyleName});<br />}<br />//Apply the style to each instance of the tag.<br />app.changeTextPreferences.appliedParagraphStyle = myStyle;<br />myStory.changeText();<br />//Reset the changeTextPreferences.<br />app.changeTextPreferences = NothingEnum.nothing;<br />//Set the changeTo to an empty string.<br />app.changeTextPreferences.changeTo = "";<br />//Search to remove the tags.<br />myStory.changeText();<br />//Reset the find/change preferences again.<br />app.changeTextPreferences = NothingEnum.nothing;<br />}<br />}<br />//Reset the findGrepPreferences.<br />app.findGrepPreferences = NothingEnum.nothing;<br />}<br />function myRemoveDuplicates(myArray){<br />//Semi-clever method of removing duplicate array items; much faster<br />//than comparing every item to every other item!<br />var myNewArray = new Array;<br />myArray = myArray.sort();<br />myNewArray.push(myArray[0]);<br />if(myArray.length > 1){<br />for(var myCounter = 1; myCounter < myArray.length; myCounter ++){<br />if(myArray[myCounter] != myNewArray[myNewArray.length -1]){<br />myNewArray.push(myArray[myCounter]);<br />}<br />}<br />}<br />return myNewArray;<br />}<br /><br />No Response with my code,but no bug, i don't know why?
                    • 7. Re: Using Grep to find text pattern, get found text, and get page number found on
                      robin521 Level 1
                      function myReadPMTags(myStory){<br />var myName, myString, myStyle, myStyleName;<br />var myDocument = app.documents.item(0);<br />//Reset the findGrepPreferences to ensure that previous settings<br />//do not affect the search.<br />app.findGrepPreferences = NothingEnum.nothing;<br />app.changeGrepPreferences = NothingEnum.nothing;<br />//Find the tags (since this is a JavaScript string,<br />//the backslashes must be escaped).<br />app.findGrepPreferences.findWhat = "[^A-Za-z0-9/.-]+";<br />var myFoundItems = myStory.findGrep();<br />if(myFoundItems.length != 0){<br />var myFoundTags = new Array;<br />for(var myCounter = 0; myCounter<myFoundItems.length; myCounter++){<br />myFoundTags.push(myFoundItems[myCounter].contents);<br />}<br />myFoundTags = myRemoveDuplicates(myFoundTags);<br />//At this point, we have a list of tags to search for.<br />for(myCounter = 0; myCounter < myFoundTags.length; myCounter++){<br />myString = myFoundTags[myCounter];<br />//Find the tag using findWhat.<br />app.findTextPreferences.findWhat = myString;<br />//Extract the style name from the tag.<br />myStyleName = myString.substring(1, myString.length-1);<br />//Create the style if it does not already exist.<br />try{<br />myStyle = myDocument.paragraphStyles.item(myStyleName);<br />myName = myStyle.name;<br />}<br />catch (myError){<br />myStyle = myDocument.paragraphStyles.add({name:myStyleName});<br />}<br />//Apply the style to each instance of the tag.<br />app.changeTextPreferences.appliedParagraphStyle = myStyle;<br />myStory.changeText();<br />//Reset the changeTextPreferences.<br />app.changeTextPreferences = NothingEnum.nothing;<br />//Set the changeTo to an empty string.<br />app.changeTextPreferences.changeTo = "";<br />//Search to remove the tags.<br />myStory.changeText();<br />//Reset the find/change preferences again.<br />app.changeTextPreferences = NothingEnum.nothing;<br />}<br />}<br />//Reset the findGrepPreferences.<br />app.findGrepPreferences = NothingEnum.nothing;<br />}<br />function myRemoveDuplicates(myArray){<br />//Semi-clever method of removing duplicate array items; much faster<br />//than comparing every item to every other item!<br />var myNewArray = new Array;<br />myArray = myArray.sort();<br />myNewArray.push(myArray[0]);<br />if(myArray.length > 1){<br />for(var myCounter = 1; myCounter < myArray.length; myCounter ++){<br />if(myArray[myCounter] != myNewArray[myNewArray.length -1]){<br />myNewArray.push(myArray[myCounter]);<br />}<br />}<br />}<br />return myNewArray;<br />}
                      • 8. Re: Using Grep to find text pattern, get found text, and get page number found on
                        Peter Kahrel Adobe Community Professional & MVP
                        Robin,

                        To make your code examples readable, put <pre> at the beginning of your code examples and </pre> at the end, and replace < with &lt;.

                        Your GREP is faulty: instead of [^A-Za-z0-9/.-] use [^A-Za-z0-9\\.-]

                        Peter
                        • 9. Re: Using Grep to find text pattern, get found text, and get page number found on
                          robin521 Level 1
                          Hi Peter,<br /><br />Thank you, <br /><br />&lt;pre>function myReadPMTags(myStory){ <br />var myName, myString, myStyle, myStyleName; <br />var myDocument = app.documents.item(0); <br />//Reset the findGrepPreferences to ensure that previous settings <br />//do not affect the search. <br />app.findGrepPreferences = NothingEnum.nothing; <br />app.changeGrepPreferences = NothingEnum.nothing; <br />//Find the tags (since this is a JavaScript string, <br />//the backslashes must be escaped). <br />app.findGrepPreferences.findWhat = "[^A-Za-z0-9/.-]+"; <br />var myFoundItems = myStory.findGrep(); <br />if(myFoundItems.length != 0){ <br />var myFoundTags = new Array; <br />for(var myCounter = 0; myCounter<myFoundItems.length; myCounter++){ myFoundTags.push(myFoundItems[myCounter].contents); } myFoundTags = myRemoveDuplicates(myFoundTags); //At this point, we have a list of tags to search for. for(myCounter = 0; myCounter < myFoundTags.length; myCounter++){ myString = myFoundTags[myCounter]; //Find the tag using findWhat. app.findTextPreferences.findWhat = myString; //Extract the style name from the tag. myStyleName = myString.substring(1, myString.length-1); //Create the style if it does not already exist. try{ myStyle = myDocument.paragraphStyles.item(myStyleName); myName = myStyle.name; } catch (myError){ myStyle = myDocument.paragraphStyles.add({name:myStyleName}); } //Apply the style to each instance of the tag. app.changeTextPreferences.appliedParagraphStyle = myStyle; myStory.changeText(); //Reset the changeTextPreferences. app.changeTextPreferences = NothingEnum.nothing; //Set the changeTo to an empty string. app.changeTextPreferences.changeTo = ""; //Search to remove the tags. myStory.changeText(); //Reset the find/change preferences again. app.changeTextPreferences = NothingEnum.nothing; } } //Reset the findGrepPreferences. app.findGrepPreferences = NothingEnum.nothing; } function myRemoveDuplicates(myArray){ //Semi-clever method of removing duplicate array items; much faster //than comparing every item to every other item! var myNewArray = new Array; myArray = myArray.sort(); myNewArray.push(myArray[0]); if(myArray.length > 1){ <br />for(var myCounter = 1; myCounter < myArray.length; myCounter ++){ if(myArray[myCounter] != myNewArray[myNewArray.length -1]){ myNewArray.push(myArray[myCounter]); } } } return myNewArray; } <br />&lt;/pre>
                          • 10. Re: Using Grep to find text pattern, get found text, and get page number found on
                            Peter Kahrel Adobe Community Professional & MVP
                            Don't replace the < in <pre> and </pre> with &lt;
                            Do replace "[^A-Za-z0-9/.-]+" with "[^A-Za-z0-9\\.-]+"

                            Peter