Skip navigation
Currently Being Moderated

[js CS5] Finding unknown text using GREP

Apr 13, 2012 8:13 AM

Tags: #cs5 #javascript #indesign_cs5 #scripting #grep

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?

  • Currently Being Moderated
    Apr 14, 2012 9:31 AM   in reply to T_Stevens

    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 = "...";



    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points