DataLinker and scripting

With InDesign, it’s easy to create a script that modifies DataLinker elements using an InDesign feature called XML Rules. An XML rule is a block of scripting code that defines three elements:

  1. A name (as a string).
  2. A condition (as a string).
  3. An action (as a scripting function).

The condition string uses the XPath specification to match elements according to their tag names, their attributes, or their siblings. The action is a script function that is passed the XML element as a parameter. All of the examples we show here are written in JavaScript, but you can also write XML Rule scripts using AppleScript or VBScript.

Since all of DataLinker’s elements are merged into the document as an XML element with attributes to identify the data source and the column from the database, you can use the condition part of the XML rule to find items from a specific column in your data source. You can then use the action to check the actual content of the data and take an action depending on the data. You have the full power of InDesign’s scripting API to do whatever you want to the document itself.

You can create any number of XML Rules to operate on your DataLinker data. Once they are created, you collect them together into a rule set and run the rule set on the set of XML elements you would like to modify.

Below is a sample script. Additionally, all of the actions in the "Utility" menu under the DataLinker menu are implemented as scripts, and they're available to be browsed and modified. You can find them in your InDesign folder, under Scripts > DataLinker.

The following sample script will perform some action on any element in the document that comes from the "department" field.

// The "glue code.jsx" file is provided by Adobe, and can be found in InDesign’s Scripts folder
#include “glue code.jsx”

// This example has one rule, but you can have as many as you want
// in the array
var myRuleSet = new Array(new RuleName);
var myDocument = app.documents.item(0);
// process the rule set on an XML node, in this case the root node

// Here is the sample XML rule:
function RuleName() { = “RuleName”;
this.xpath = “//teacup-tag[@column-name=’department’]”;
this.apply = function (element, ruleSet, ruleProcessor){
//Do something here.
//Return true to stop further processing of the XML element
return true;
}; // end of rule function   

Details of how to write an XPath condition and examples of actions are all contained in InDesign’s scripting guide. Here is a hyperlink to Adobe’s scripting support page, where you can find the above referenced guid plus detailed documentation and examples of using XML Rules. You can also see the fourth walkthrough in our walkthrough tutorial for a functional example of how to use XML Rules with DataLinker.

Have more questions? Submit a request


Please sign in to leave a comment.