4 Replies Latest reply on Jun 9, 2009 8:06 AM by Thomas B. Nielsen

    Grep with Stories

    Justin Monsees
      I am currently trying to use the GREP feature using JS for Indesign. I got it working at the document level but I want to be able to search and replace things at the textFrame level. Looking at the interface, after you select a textFrame, you can set the "Search" option to "Story." How do I code this.


      Justin M.
        • 1. Re: Grep with Stories
          Kasyan Servetsky Level 5
          Assuming that a text frame is selected:
          var myTextFrame = app.selection[0];
          var myStory = myTextFrame.parentStory;
          app.findGrepPreferences = app.changeGrepPreferences = null;
          app.findGrepPreferences.findWhat = "(?i)[A-Z]*?@[A-Z]*?[.]...";
          app.changeGrepPreferences.underline = true;
          app.findGrepPreferences = app.changeGrepPreferences = null;

          You can search and replace in a story:
          and in a text frame:
          • 2. Re: Grep with Stories
            Thomas B. Nielsen Level 3

            Is there a way to search in app.selection, if the selection has multiple textFrames?


            Ex: I have selected two textframes and would like the script to return all grep mathces of the contents of the two frames.

            • 3. Re: Grep with Stories
              Dave Saunders Level 4

              If those are two (or more) separate text frames, you'll need search the text of each of them. A script might look like this:


              if (app.documents.length > 0 &&
                          app.selection.length > 0) {
                  var myPIs = app.selection;
                  for (var j = myPIs.length - 1; j >= 0; j--) {
                      if (myPIs[j] instanceof TextFrame) {


              Where the goSearch function (which I'll leave you to write) does the actual grepping of each selected text frame. Note: this code will work even if only one text frame is selected.



              • 4. Re: Grep with Stories
                Thomas B. Nielsen Level 3

                Thanks Dave,


                I got it working by using concat to get the results into one array - but I was hoping you could do something like mySearchItems.everyItem().findGrep() but this will do, thank you.