Coding my first InDesign script

Every month, a small team and I design a cooking magazine. Laying out all the recipes is a labor-intensive job. That's why I started searching for a way to automate and simplify the process.

After trying a while with GREP expressions and Paragraph Styles, it became clear that wouldn’t be enough. This gave me the opportunity to set my first small steps into InDesign Scripts and write my first small script. I couldn’t find much information for beginners on the internet and had never written in Javascript before. Therefore, it took me quite a while to come up with these 11 lines of code. Fortunately, this lost time was quickly made up for.

Basically, the script detects the different parts of the recipe and gives them the right Paragraph Style.

This GIF demonstrates all the different steps.

The recipes are sent to us in separated word-files with the same structure every time. The number of steps and ingredients depends on the recipe.

Title¶
Subtitle¶

Ingredient¶
Ingredient¶
Ingredient¶
Ingredient¶
…¶

1.»step 1¶
2.»step 2¶
3.»step 3¶
4.»…

I will briefly explain how the script works.

The first two rules create the necessary variables. The first line ensures that the script runs on the selected text frame. When the recipe flows across multiple text frames, the second line makes sure the script follows.

var Selection = app.selection[0];
var Recepi = Selection.parentStory;

The ingredients are the hardest part to detect because they do not contain unique text properties. To solve this, first put the complete recipe in the Paragraph Style Ingredients. Next start searching for the the steps, title and subtitle.

Recepi.appliedParagraphStyle = "Ingredients";

A white space is always on top and bottom of the ingredients. These are detected by this GREP expression: “(? <= ~ B) ~ b”. They receive the Paragraph Style Steps, the same style the steps will get.

app.findGrepPreferences.findWhat = "(?<=~b)~b";  
app.changeGrepPreferences.appliedParagraphStyle = "Steps";
Recepi.changeGrep();

All the steps can be detected by their tab(»). Use “.\T” to search for a point followed by a tab.

app.findGrepPreferences.findWhat = ".\t";  
app.changeGrepPreferences.appliedParagraphStyle = "Steps";
Recepi.changeGrep();

With the next 2 steps, the first and second paragraph receives the style Title and Subtitle.

Recepi.paragraphs[0].appliedParagraphStyle = "Title"; 
Recepi.paragraphs[1].appliedParagraphStyle = "Subtitle";

All parts now have the right Paragraph Style assigned and the script is ready.

var Selection = app.selection[0];
var Recepi = Selection.parentStory;
Recepi.appliedParagraphStyle = "Ingredients"; 

app.findGrepPreferences.findWhat = "(?<=~b)~b";
app.changeGrepPreferences.appliedParagraphStyle = "Steps";
Recepi.changeGrep();
app.findGrepPreferences.findWhat = ".\t";  
app.changeGrepPreferences.appliedParagraphStyle = "Steps";
Recepi.changeGrep();
Recepi.paragraphs[0].appliedParagraphStyle = "Title"; 
Recepi.paragraphs[1].appliedParagraphStyle = "Subtitle";

Thanks to some additional GREP expressions in the Paragraph Styles, we can now get the full recipe styled in a simple mouse click.

So now you know how it’s done. Let’s start cooking.