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.

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: