Automate GREP Find/Change in InDesign with ChainGREP
Editor’s note: Previously, we shared the news about ChainGREP, a great script that allows you to save shareable custom sets of GREP queries as scripts. This is a follow-up post by the author of that script, to fully explain how it works.
Making GREP easier to use, more flexible and efficient
For most folks, walking 20 feet (6 meters) takes no effort at all. But imagine trying to jump that same distance in one leap. A lot harder, right? It would take a lot of training, and most of us would still never get anywhere near that. Using GREP is very similar. A GREP newbie can sometimes accomplish the same thing as a GREP master by breaking a task into several small steps. That was the first idea behind ChainGREP: to use several simple GREP replacements and execute them one after the other to do the work of a single complex GREP.
Also, when your GREP Find/Change is composed of several independent operations, you can use them interchangeably, making different combinations to accomplish different tasks.
The only problem is when you need to run the same Find/Change operation often, it’s tedious and inefficient to select and execute several queries again and again. To string queries together and run them all, you could use the FindChangeByList script that comes with InDesign, but it is not very user-friendly and hard for most folks to master.
We need a script to the rescue!
Running different queries in a row is a great job for a script. However, like GREP mastery, scripting is a skill that not everyone possesses. So I wrote a script called ChainGREP.jsx that allows you to create your own new scripts that run several GREP queries in a row. And you don’t need to know anything about scripting to use ChainGREP!
How to use ChainGREP
1. Create queries
The first and most important step is to save the individual GREP queries in the Find/Change dialog box. To do this, create the GREP query (including any desired format settings), and save it by clicking on the hard disk icon. Give your query a name that clearly describes what it does. You will need it later for selecting the query.
2. Download the script
After downloading the script, you need to install it. If you need instructions, read this detailed installation guide. As a brief reminder: open the Scripts panel, right-click on the User folder and select Reveal in Finder/Explorer, then copy script to folder Scripts panel and you’re good to go.
3. Run the script
Double-click the ChainGREP script in the Scripts panel and a dialog box appears, showing all your saved GREP queries.
Your entire list of queries can be stored and run in one script. But it’s more likely that you’ll only want to run a selection of queries, one after the other. To do this, use Remove from List to delete queries from the selection. Then, put the queries in the order you want them to run with the Up or Down buttons.
Next, set the scope for the new script in Scope of Find/Change. In most cases, searching through the document is a good choice.
Then enter a name for the script.
Finally, click Save List and the new script is saved in the FindChangeScripts subfolder, which is created next to the script.
Unfortunately, the script does not always appear automatically in InDesign CC; sometimes you have to close and reopen the Scripts panel. Then the new script will appear and you can double-click it to run it.
Your scripts are portable
In the script, the queries are saved with all settings. This means you can also move the script to another location or use it on another computer where the queries are not installed. This also means that if the original GREP query is changed, the script must be re-created.
Write your own script
If you would rather write your own script, you can open the script and analyze the code – it is all open source. If you can read German, I recommend my book InDesign automatisieren, which contains a comprehensive overview of Find/Change operations with GREP. Currently, there is no English version of the book.
Finally, many thanks to Peter Kahrel, who gave me the idea for this script with his script GREP query manager.