Finding XML elements. Adobe InDesign CS6
Advertisement
Advertisement
C HAPTER 13: XML Rules XML Rules Examples 226 end ReturnTrue to ReturnFalse() script ReturnFalse property name : "ReturnFalse" property xpath : "//*" on apply(myXMLElement, myRuleProcessor) global myReturnString set myReturnString to "OK" tell application "Adobe InDesign CS6" try tell myXMLElement set fill color of text 1 to "ColorB" end tell on error myError set myReturnString to myError end try end tell
--Leave the XML element available for further matching rules.
return false end apply end script end ReturnFalse
Finding XML elements
As noted earlier, the subset of XPath supported by XML rules does not allow for searching the text contents of XML elements. To get around this limitation, you can either use attributes to find the XML elements you want or search the text of the matching XML elements. The following script shows how to match XML elements using attributes. This script applies a color to the text of elements it finds, but a practical script would do more. For the complete script, see FindXMLElementByAttribute.
global myGlueCode on run tell application "Adobe InDesign CS6" set myFilePath to file path as string set myFilePath to myFilePath & "Scripts:Xml rules:glue code.scpt" tell document 1 set myRootXML to first XML element set myColorA to make color with properties
{name:"ColorA", model:process, color value:{0, 100, 80, 0}} end tell end tell set myGlueCode to load script file myFilePath set myRuleSet to {my AddAttribute()} tell myGlueCode
__processRuleSet(myRootXML, myRuleSet, {}) end tell set myRuleSet to {my FindAttribute()} tell myGlueCode
__processRuleSet(myRootXML, myRuleSet, {}) end tell end run to AddAttribute() script AddAttribute property name : "AddAttribute" property xpath : "/devices/device/part_number"
--Adds the content of an XML element to an attribute of
C HAPTER 13: XML Rules XML Rules Examples 227
--the parent of the XML element. This can make finding the
--element by its contnet much easier and faster.
on apply(myXMLElement, myRuleProcessor) global myReturnString set myReturnString to "OK" tell application "Adobe InDesign CS6" try tell myXMLElement set myString to contents of text 1 tell parent make XML attribute with properties
{name:"part_number", value:myString} end tell end tell on error myError set myReturnString to myError end try end tell return true end apply end script end AddAttribute to FindAttribute() script FindAttribute property name : "FindAttribute" property xpath : "/devices/device[@part_number = 'DS001']" on apply(myXMLElement, myRuleProcessor) global myReturnString set myReturnString to "OK" tell application "Adobe InDesign CS6" try tell myXMLElement set fill color of text 1 to "ColorA" end tell on error myError set myReturnString to myError end try end tell return false end apply end script end FindAttribute
The following script shows how to use the findText
method to find and format XML content (for the complete script, see FindXMLElementByFindText):
C HAPTER 13: XML Rules XML Rules Examples 228 global myGlueCode on run tell application "Adobe InDesign CS6" set myFilePath to file path as string set myFilePath to myFilePath & "Scripts:Xml rules:glue code.scpt" tell document 1 set myRootXML to first XML element set myColorA to make color with properties {name:"ColorA", model:process, color value:{0, 100, 80, 0}} end tell end tell set myGlueCode to load script file myFilePath set myRuleSet to {my FindByFindTExt()} tell myGlueCode
__processRuleSet(myRootXML, myRuleSet, {}) end tell end run to FindByFindTExt() script FindByFindTExt property name : "FindByFindText" property xpath : "/devices/device/description" on apply(myXMLElement, myRuleProcessor) global myReturnString set myReturnString to "OK" tell application "Adobe InDesign CS6" set find text preferences to nothing set change text preferences to nothing set find what of find text preferences to "triangle" try tell myXMLElement set myFoundItems to find text if (count myFoundItems) is greater than 0 then set fill color of text 1 to "ColorA" end if end tell on error myError set myReturnString to myError end try set find text preferences to nothing set change text preferences to nothing end tell return false end apply end script end FindByFindTExt
The following script shows how to use the findGrep method to find and format XML content (for the complete script, see FindXMLElementByFindGrep):
Download
Advertisement