Skip to main content
Participating Frequently
May 6, 2019
Answered

Alternate Ahead color using Grep?

  • May 6, 2019
  • 1 reply
  • 3036 views

Hello I'm formatting a book in which the Aheads need to alternate color in a pattern.

As of right now I am manually applying a character style to color each Ahead but I'm wondering if there is a way to alternate them automatically using GREP. I have seen people use GREP to alternate the color of letters in a paragraph, is there a way to alternate each instance of a specific paragraph style? I cannot use the [Repeat] option in Nested styles or the Next Style option because the aheads have other paragraph styles in-between them.

This topic has been closed for replies.
Correct answer Peter Kahrel

Yes, each recipe title has the Ahead style applied to it. The layouts above are just an example. There are 30 recipes total. As I said in my initial post, right now to get the different colors in the Aheads, I'm manually applying the colors to every third ahead with a character style but I'd like it if I could GREP it in, that way even if the recipes move around the formatting stays correct within the pattern.

Much the way the letters in this example cycle through colors: Indiscripts :: Cyclic GREP Styles

But instead of alternating color every letter I want it to do it in each instance of the paragraph style.


Because the scope of a GREP style is strictly a single paragraph, you can't use GREP styles to achieve what you want. But it's a simple script. Given that recipe headings are in a paragraph style Ahead and given three character styles green, blue, and red, the following script applies them in order:

cstyles = ['green', 'blue', 'red'];

app.findGrepPreferences = null;

app.findGrepPreferences.appliedParagraphStyle = app.documents[0].paragraphStyles.item('Ahead');

heads = app.documents[0].findGrep();

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

   heads.appliedCharacterStyle = cstyles[i%3];

}

To change the order in which the character styles are applied, change the order in the list in the first line.

You may have to change the scope of the search from document to selected story, depends on how your document was set up.

Peter

1 reply

FRIdNGE
May 6, 2019

If your limit is 99, sure, play it with Grep styles!

^Ahead + "0-green" char style

^2nd.+ + 2-blue cs

^3rd.+ + 3-red cs

Good luck!

Participating Frequently
May 7, 2019

How would I enter that into the Apply style: and To Text: fields? And how would I make the pattern repeat so it'd be Green, Blue, Red, Green, Blue, Red? Also to be clear these Ahead will be recipe names, not actually say 2nd Ahead, etc.

FRIdNGE
May 7, 2019

One Doc? One Story? A Story Per Recipe? Data Merging?