2 Replies Latest reply: Apr 16, 2012 3:29 PM by T_Stevens RSS

    [js CS5] Finding unknown text using GREP

    T_Stevens Community Member

      I'm very new to GREP, and somewhat new to InDesign scripting so bear with me if I seem ignorant.

       

      I'm trying to create a script that will match on a certain paragraph and tag/style it for use in a running head. The paragraph may be, for instance: 'Unit: Plants,' and I want the running head to just be 'Plants.' Up to this point I've just been going through the book and tagging each unit title by hand, but I would like a script that will go through and tag all the text following the  'Unit' paragraph. The problem is that the book I'm working on isn't in English, it's in about 15 different languages. The only clue I have on what to tag is that at the beginning of each unit there are a couple short English paragraphs to let me know what unit I'm working on, like so:

      [last paragraph of previous unit]

      Unit: Food for the Body and Mind (full unit title)

      Unit: Food (short title)

      Ushqim (short title in the language for the running head)

      Ushqim per trupin dhe mendjen (full title in language)

      [first paragraph of Food unit]

       

      I have an array of all the unit titles in English, and I was thinking that I could do a GREP find for the short title and a hard return and the entire following paragraph like this:

      var langUnit = new Array();

      for(i = 0; i < myArray.length; i++){

        app.findGrepPreferences.findWhat = "Unit: "+myArray[i]+"\\r.*?\\r";

        langUnit.push(myDoc.findGrep()); }

       

      My first thought was that I could then split the resulting strings on the hard return and tag the second element of the resulting array, of course I didn't realize that the hard returns would throw that off, and now I'm stuck.

      Let me know if any of this doesn't make sense.

       

      Any ideas on how I can proceed?

        • 1. Re: [js CS5] Finding unknown text using GREP
          pkahrel CommunityMVP

          Your expression "Unit: "+myArray[i]+"\\r.*?\\r" matches three paragraphs, so you can address paragraphs[2] to access just the short title. It seems to me that you don't need to look for the English short titles first: simply look for two consecutive paragraphs that start with "Unit:", then take the paragraph that follows them. It's similar to what you do, but simpler:

           

          app.findGrepPreferences = null;

          app.findGrepPreferences.findWhat = "Unit:.+\\rUnit.+\\r.+";

          myFinds = app.activeDocument.findGrep();

          for (i = 0; i < myFinds.length.length; i++)

             myFinds[i].paragraphs[2].appliedCharacterStyle = "...";

           

          Peter

          • 2. Re: [js CS5] Finding unknown text using GREP
            T_Stevens Community Member

            Thanks for your response Peter. Your snippet is simpler than what I was trying. I don't know why, but I was getting similar results with your snippet as I was with my own. For some reason, however, after completely re-typing the code in my script editor it worked great.