How to Apply GREP Quickly with InDesign Scripts

This entry is part 8 of 18 in the series Tutorials

Introduction

If you’ve been following Tutorial Tuesdays lately, you’ll remember that we recently learned how to apply Paragraph Styles to a manuscript with markup using GREP. Even though GREP is a largely automated process, we were still applying the styles one at a time through the Find / Change dialog, and that’s time-consuming

I hinted at a way to automate the process of applying styles using GREP and some of InDesign’s powerful built-in Scripts. This week, I’ll show you how to do that.

But First!

If you’d like to follow along, download the demonstration file and import it to InDesign. Even if you downloaded the file last week, you might want to download it again; I updated it with a few new Character Styles that we’ll be using in today’s example.

MKM Circus Craziness Tutorial File
MKM Circus Craziness Tutorial File
MKM Circus Craziness Tutorial File.docx
18.4 KiB
659 Downloads

GREP and Character Styles

Last week we focused on the GREP to apply Paragraph Styles. Applying Character Styles is nearly identical, but because they apply to only a portion of a paragraph, we need to use closing markup tags to denote where the character style should end.

Markup for a Paragraph Style might look like this:

<h1>Chapter 1: History

But markup for a Character Style would look like this:

Text not to be in bold. <b>Text to be in bold.</b> More text not to be in bold.

Recall that the GREP expression to find a Paragraph Style looks like this:

(<h1>)(.+)

To find the text between two tags, use the following GREP expression:

(<b>)(.+?(</b>)

You’ll still insert a single $2 in the Change To field.

In our expression, the question mark tells InDesign to stop searching after it finds the shortest match of characters that satisfies the criteria. If you don’t include it and you have more than one set of <b> and </b> tags in the same paragraph, InDesign will select all text between the first <b> tag and the last </b> tag. In short, you’ll get too much text and wind up creating more bold text than you wanted.

If you’re playing along, feel free to try out this expression using the file you downloaded. Then try the expression without the question mark and observe the difference.

Correct example (with question markJ

Incorrect example (without question mark):

Automating GREP with Scripts

Now that you’ve had a crash course in using Character Styles, let’s get down to business. InDesign has tons of built-in Scripts that can be used to automate a lot of different tasks, and thankfully applying Styles is one such task it can automate.

To access the Scripts panel from InDesign:

1. Select Window -> Utilities -> Scripts or press ALT + CTRL + F11.


2. The Scripts Panel appears.

Setting Up the Script

We’ll be using an Application Script named “FindChangeByList.jsx.” Before we can begin using it though, we need to set it up. To do so, we’ll need to open the text file the script uses to get its information about which tags and styles we wish to find and change.

We can launch that text file from within InDesign. On the Scripts Panel, do the following:

1. Click the Down Arrow next to Application, then the arrow next to Samples and finally the arrow next to JavaScript. A list of InDesign’s packaged JavaScripts appears.

2. Scroll through the list until you find a script named FindChangeByList.jsx.

3. Underneath that entry, there is a folder called FindChangeSupport. Right-click that folder and select Reveal in Explorer.

This step may vary somewhat if you’re using InDesign on a Mac.

4. A folder opens on your computer. If you’re using Windows, it opens in Windows Explorer. Double-click on the folder called FindChangeSupport. Inside is a file called FindChangeList.txt.

This may vary somewhat from computer to computer too. If you don’t have Explorer set to show extensions, for example, it may only be called FindChangeList.

5. Create a backup copy of FindChangeList.txt. The easiest way to do so is to copy it, paste it into the same folder, then rename it to something like FindChangeList-Backup.txt.

6. Open the text file.

You’ll need to edit and add several lines to the bottom of this text file. The comments at the beginning of the file attempt to explain what everything means, but really it’s just a bunch of jargon.

It’s difficult to know exactly what to add at this step. I found another script on the internet that created the code for me. It’s called RecordFindChange_CS3-CS6.jsx and you can download it from the following URL:

http://colecandoo.wordpress.com/category/fonts-type/

Unzip the file into your JavaScript folder (the folder launched when you clicked Reveal in Explorer from InDesign). To run this script, enter all of the information you need for a single GREP Find/ Change into the Find / Change Dialog in InDesign, then run the script from the Scripts Panel by double-clicking its name. A Notepad file will open with the code you need to add to the end of the FindChangeList.txt file.

Alternatively, you can download my copy of the file and simply edit the text. This will save you a lot of time, since I’ve already done the work.

FindChangeList-txt
FindChangeList-txt
FindChangeList.txt
4.4 KiB
1427 Downloads

7. Look for the section at the bottom of the FindChangeList.txt file where each of the new lines start with the word “grep.”

You’ll see several lines that look like this:

grep    {findWhat:”(<h2>)(.+)”}    {changeTo:”$2″, appliedParagraphStyle:”H2″}    {includeLockedStoriesForFind:false, includeLockedLayersForFind:false, includeHiddenLayers:true, includeMasterPages:true, includeFootnotes:true, kanaSensitive:true, widthSensitive:true}

9. Don’t freak out.

10. Let’s break this down a little bit.

“grep” tells the code to use the GREP tab of the Find / Change Dialog.

{findWhat:”(<h2>)(.+)”}    probably looks familiar. It’s the GREP expression you enter into the Find / Change Dialog in InDesign. You can make changes to this section just as if you were changing your expression within InDesign. Just change the <h2> tag to read <h3> or <term> or whatever other opening tag you want to use.

{changeTo:”$2″, appliedParagraphStyle:”H2″} probably looks familiar too. It’s everything you would enter into the Change To line, as well as the Change Format field within InDesign. You can edit the parts that look familiar here too, as long as you don’t remove the quotation marks. For example, you can change “H2” to read “H3” and apply the H3 Paragraph Style instead of the H2 style.

The rest of the code doesn’t matter. It just describes a bunch of the other check boxes found in the Find / Change Dialog.

11. Edit each of the lines in the text file to match your tags (such as <h1>, <h2>, <term>, and <b></b>). Also, change the appliedParagraphStyle:”H2″} parameter to match. For example, if you change the first tag to <h3>, change the H2 in this section to read H3 instead.

12. You’ll notice a difference between the Paragraph Styles and the Character Styles. It’s very similar, but the changeto section reads appliedCharacterStyle:”Bold”} instead. Change these to match your needs as well.

13. If you need to apply more styles, simply copy a line, paste it to the line below, and edit as desired. Each line represents one Find/Change action, so you need to create one line per Paragraph and Character Style you wish to apply.

14. After creating and saving all of the entries you need, save and close the text file.

Your script is now ready to run.

Run the FindChangeByList Script

Now you’re ready to run the script and apply your styles. Switch back to InDesign and do the following:

1. In the Scripts Panel, find the FindChangeByList.jsx option again.

2. Double-click on the name of the script.

3. Magic happens.

4. Congratulate yourself on becoming an InDesign Wizard.

Final Thoughts

It seems tricky at first, but if you follow these steps you won’t have to perform the hours of research and testing it took me to figure this out myself. If you get stuck or lost along the way, you can ask me a question about the process at any time.

We’ve now set up a script to help us automate our workflow. It seemed like a lot of effort, but consider this:

If you always markup your manuscripts using the same tags,

and if you set up your script properly just one time,

then all you have to do is copy your future manuscripts into InDesign and double-click on a script to format all of the styles quickly and easily.

Using the methods we’ve discussed over the last several weeks, you can set up and automate several, dozens, or even hundreds of documents with little effort. How cool is that?

And now that you’ve dipped your toes into the cool, refreshing waters of GREP, you won’t be able to go back to manually formatting everything. Next week, I’ll share some of my favorite GREP tricks with you.

Donate

Do you enjoy these Accessible Games Tutorials? They’ll always be free to you, but if you’d like to leave a token of appreciation than please consider donating.

Donate through our Gumroad Store.

Or Buy our Games.

Did you know I’m also available for hire? Contact me if you have a layout project that you’re not ready to tackle yourself.

Indie+

This post also appears on Indie+ and is covered by the Indie+ Community Standards.

Indie+ Logo

Series Navigation<< The Secret to Simple Manuscript Import with InDesign CS6Making Magic Happen with GREP >>