Getting Architecture Done
Efficient Methods for Architecture

Set InDesign Variables with JSON

As I previously mentioned in Efficient Methods for Architectural Graphics, making your own InDesign document templates is a really good practice — it you will save time when creating similar documents, and it will let you focus on the important part of your document: its content.

When document templates are comprised of multiple text variables, it can be tedious to manually edit all text fields to produce new documents, especially when working with repetitive forms (such as spreadsheets, receipts, or reports).

Text Variables

InDesign allows to create text variables to hold different values of your document as variables. These variables can then be inserted into one or more parts of your document. For instance, you could create a text variable named Name, then insert it wherever your name has to appear in that document. Later on, by modifying the value of the text variable Name, your document would replace all the instances of your name to whatever new name you set.

Text Variables can be created through the menu under Type > Text Variables > Define.., and inserted into a document's text through Type > Text Variables > Insert Variable — only when you are inside a text frame.

Changing Text Variable's Values with JSON

Once your document is set with your desired text variables (let's say we are using two variables named Title and Subtitle, for this example's sake), you can either change their values manually or automate the process by running the script below — this way, your text fields will get filled with data using a JSON object, named settings in this example.

var settings = {title: 'Title',
                    subtitle: 'Your Subtitle Here'};

app.activeDocument.textVariables.item("Title").variableOptions.contents = settings.title;
app.activeDocument.textVariables.item("Subtitle").variableOptions.contents = settings.subtitle;

For those of you who do not code, JSON means JavaScript Object Notation, and it is not more than a syntax to store information. For this script, only simple JSON objects contained in between { and } are needed; writing first the variable name (or key), then a colon, and then the variable's value. Variables need to be separated with commas, and you can add as many as you want. And, in case you have never ran a JavaScript script on InDesign, I will soon explain how to in a future post.