Chaining Expressions

We've seen that it is often preferable, and sometimes even necessary, to use more than one expression to achieve something. But unfortunately it is not possible in GREP's interface to chain two or more expressions, so you may find yourself running saved queries in succession. However, to get around this, we can create a script with all related expressions and run it from the scripts panel. It may look a bit intimidating at first, but it's in fact not too complicated once you get the hang of it. Besides, the scripts shown here don't involve any real programming; they're essentially a series of statements. In that respect they're more like actions.

The scripts illustrated here are in JavaScript, which can be used both on Macs and Windows. The first script combines four expressions that we illustrated earlier, namely the two that replace hyphens with en dashes in page ranges, and the two that drop digits from ranges. In the first script, below, the first two lines clear the tab of any settings, after which we include a line to include footnotes in the replacements. Then there are four sets of three lines, and once you work your way past JavaScript's verbosity you can easily see the expressions that we've been dealing with in this Short Cut. Note that the backslash must be escaped, which is why you see the double backslashes. You could easily use this script as a template for other expressions:

// Reset the GREP tab app.findGrepPreferences = null; app.changeGrepPreferences ...

Get GREP in InDesign CS3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.