EasyCatalog supports an embedded version of the Lua programming language. When text is processed, any commands between delimiters [[ and ]] are processed using Lua. EasyCatalog supplements Lua with functions to make data access easier. For example:
1 | [[if (field('new') == '1') then;]]NEW![[end;]] |
In this example, if the contents of the ‘new’ field are equal to ‘1’ the text NEW! will be output. Any InDesign text based content can be entered between the delimiters, such as anchored objects.
Working With The Selection
EasyCatalog uses the SELECTION class to inspect, set and navigate the selection. A call to SELECTION.root() retrieves the root SELECTION object. This represents what’s dragged to a frame or paginated. This has several methods, size() returns the number of records in the selection and get() retrieves the item for use . Here’s an example that calls a function that outputs NEW! for each record in the SELECTION based on a flag:
1 2 3 4 5 6 7 8 9 10 | [[function record() if (field('new') == '1') then;]]NEW![[end; end i = 1; sel = SELECTION.root(); while i <= sel:size() do; sel:get(i); record(); i = i + 1; end]] |
Lua is pseudo object oriented. SELECTION.root() returns a SELECTION object. To call methods on an instance of this object a colon should be supplied, e.g: SELECTION:size().
In the case of relational or grouped panels, the selection can be hierarchical. Here’s an example that traces the hierarchy using simple recursion:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [[function level(sel, index) if sel:size() > 0 then for i=1,index do TEXT.insert(' '); end; end; for i=1,sel:size() do sel2 = sel:get(i); TEXT.insert(sel2:type()); TEXT.insert(' - '); TEXT.insert(sel2:name()); TEXT.insert('^p'); level(sel2, index+1); end; end; level(SELECTION.root(), 0);]] |
To get the contents of a named field from the SELECTION object , the getcontent() method can be used:
1 | TEXT.insert(sel2:getcontent('key')); |
Relational Groups
For relational data sources, the SELECTION object can be used to retrieve a specific relational group using the getgroup() method:
1 2 3 4 5 6 7 8 | [[function handlenamedgroup(sel) for i=1,sel:size() do; sel:get(i); TEXT.insert(sel:name()); TEXT.insert('^p'); end; end; handlenamedgroup(SELECTION.root():getgroup('Group Name'));]] |
Inserting Content
The TEXT class as a couple of members to insert content into the document.
insert() – Inserts the given text into the text flow. Meta characters, such as ^p, ^t are interpreted:
1 | TEXT.insert('example text'); |
insertrule() – Inserts a named Formatting Rule:
1 | TEXT.insertrule('formatting rule'); |
Retrieving The Insertion Position
The text objects x(), y() and pageindex() methods return information about the current insertion. This allows page positioning based logic:
1 2 3 4 5 6 7 8 9 10 11 12 | [[function traceposition(sel) for i=1,sel:size() do sel2 = sel:get(i); TEXT.insert(TEXT.pageindex()); TEXT.insert(' - '); TEXT.insert(TEXT.x()); TEXT.insert(' x '); TEXT.insert(TEXT.y()); TEXT.insert('^p'); end; end; traceposition(SELECTION.root());]] |
Lua is also used to control the custom appearance of a fields in the data panel via the field options. Additional command are provided that are not relevant to text processing.