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.

This sample rule will perform some action on any element in the document that comes from the "department" field.

// This 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
__processRuleSet(myDocument.xmlElements.item(0),myRuleSet);

// Here is the sample XML rule:
function RuleName() {
this.name = “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 the scripting guide, and here is a hyperlink to Adobe’s scripting support page), where you can get detailed documentation and examples of using XML Rules. You can also see the fourth walkthrough in our samples for a functional example of how to use XML Rules with DataLinker.

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.