Post Processing of Formatting Rules

Once a Formatting Rule has been populated with data, it can be processed before final placement using Lua code. Typical uses for this would be to position frames relative to one another,  position a series of images,  or incorporate other Formatting Rules to build a layout. It’s also possible to create the entire content via Lua using the InDesign Scripting DOM

Frame Positioning

Here’s a simple example. The position of the box marked C should always be below box A and B irrespective of their final depth:

The post processing script is passed a formattingrule argument, which can be used to manipulate elements. Frames are identified by name, defined using the InDesign Script Label. The code to maintain the relative position of frame C is:

Frame names should generally be unique, as this allows referencing elements of an array by name. Here’s an example using this technique to achieve the same result:

All coordinates given by the getbounds method are in points relative to the top left of the rule, which has an origin of 0,0.  Frame positioning can legitimately alter the overall bounds or the Formatting Rule.  getbounds takes a variety of arguments:

Importing Formatting Rules

Other Formatting Rules can be imported and their post processing code is executed independently. Rules placed in this way are not independent, so  only update when the parent rule updates.  In this example, we insert every child elements into parent, filling left to right, top to bottom.

Parent post processing code:

Which gives the result:

Notice each child is populated using data from the given selection element. A SELECTION is a hierarchy of groups and records based on what’s assigned to the Rule. Refer to the SELECTION class for more information.

A few more examples of placerule:

The ‘require’ Keyword

When it becomes cumbersome to edit code in the standard dialog box, external modules can be created and stored as files.  The Lua command require scans a number of locations for module code. These include the ‘Scripts’ sub folder in the data source – require(“xxx”) will attempt to load “xxx.lua” from the Scripts folder. When a module cannot be found, an error is displayed listing all potential locations for the file.

Using the InDesign Scripting DOM To Create Content

The InDesign Scripting DOM can be accessed to create or modify virtually any content. So it’s possible to create a Formatting Rule that is an empty text frame, then use Lua code to generate the content for that frame:

Further information on the use of the scripting DOM can be found here.

Example using Formatting Rule Post Processing Logic to create a table which mirrors the contents of a named tabular field. The Formatting Rule is an empty text box.