Overview
Lua scripts can be integrated with EasyCatalog to provide custom menus and dialog boxes. User Interface is provided by the the InDesign Scripting DOM, which offers dialog creation and control. Details on how to access this from Lua are here.
Creating a Custom Menu Option
Adding a custom menu option is a case of creating a .lua file in the “Menus” sub folder of “Scripts” folder of a datasource. These are processed when a data source is opened. A MENU object is exposed and used to create a custom menus. ย The script is maintained in a persistent state. When working with documents it’s important to not hold onto them, otherwise a protective shutdown will occur when the document is closed.
Forcing garbage collection:
1 2 3 4 5 6 7 | local doc = app:activeDocument(); -- set to nil, to release the reference doc = nil; -- force garbage collection collectgarbage("collect"); |
Examples:
Example to Inspect the Selected Records in the Data Panel. A selectionย object is passed to the script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | function getname() return "Inspect The Selection.." end function updatestate() -- recordset = selection:getrecordset(); --if recordset:size() == 0 then return false end; return true; end function dodialog() -- get the InDesign Application Scripting DOM object app = getdom(); -- create a new dialog dlgRef = app:dialogs():add({name = "Inspect The Selection", canCancel = true , label = "Some Options To Choose"}); dlgColumn = dlgRef:dialogColumns():add(); dlgRow = dlgColumn:dialogRows():add(); -- iterate the selection, adding a new static text item for each one with the key recordset = selection:getrecordset(); for record = 1,recordset:size() do myRecord = recordset:getrecord(record); dlgColumn:staticTexts():add({staticLabel = "key = " .. myRecord:key()}); end if dlgRef:show() == true then end end -- add our menu option to the -- gettable : Function called to return the menu text. This is called each time it is displayed -- onupdate : Called to enable or disable the menu -- unselect : Function called when selected -- position : Position of the menu on the data panels pop-out menu MENU.add( { gettitle = getname, onupdate = updatestate, onselect = dodialog, position = 21 } ); |
Allow the user a choice of options. The Lua state isย initialised once per session, allowing choices to be maintained:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | chosenoption = 1; function getname() return "Chosen Option " .. chosenoption .. "..."; end function updatestate() -- true/false (enabled/disabled) return true; end function dodialog() app = getdom(); dlgRef = app:dialogs():add({name = "dlgName", canCancel = true , label = "Some Options To Choose"}); dlgColumn = dlgRef:dialogColumns():add(); dlgRow = dlgColumn:dialogRows():add(); rGroup = dlgRow:radiobuttonGroups():add(); if chosenoption == 1 then state = true else state = false; end rGroup:radiobuttonControls():add({staticLabel = "Option 1", checkedState = state}); if chosenoption == 2 then state = true else state = false; end rGroup:radiobuttonControls():add({staticLabel = "Option 2", checkedState = state}); if chosenoption == 3 then state = true else state = false; end rGroup:radiobuttonControls():add({staticLabel = "Option 3", checkedState = state}); if dlgRef:show() == true then chosenoption = rGroup:selectedButton()+1; end end -- add our menu option to the -- gettable : Function called to return the menu text. This is called each time it is displayed -- onupdate : Called to enable or disable the menu -- unselect : Function called when selected -- position : Position of the menu on the data panels pop-out menu MENU.add( { gettitle = getname, onupdate = updatestate, onselect = dodialog, position = 1 } ); |
Updating Named Fields in the Data Source
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | function getname() return "Update All Price Fields"; end function updatestate() return true; end function dodialog() -- From the Lua DATASOURCE object, get the Datasource -- object provided by the Scripting module -- (Scripting module is required to access these methods. -- would return nil when not available) ds = DATASOURCE.get():getdom(); -- Get the Application DOM app = getdom(); -- Call the method on the Scripting module to update ds:updateDocumentField(app:activeDocument(), "price1") ds:updateDocumentField(app:activeDocument(), "price2") ds:updateDocumentField(app:activeDocument(), "price3") end MENU.add( { gettitle = "getname", onupdate = "updatestate", onselect = "dodialog", position = 1 } ); |
Executing Javascript
External Scripts can be executing by accessing the InDesign scripting DOM via Lua. For example:
1 2 3 4 5 6 7 8 9 10 11 | function getname() return "Run Javascript..." end function updatestate() return true; end function doScript() getdom():doScript("/Users/Workspace/Scripts/Javascript.jsx"); end MENU.add( { gettitle = "getname", onupdate = "updatestate", onselect = doScript, position = 21 } ); |