I have a simple InDesign script - it creates a record of page properties.
tell application id "com.adobe.InDesign"
set myDoc to active document
set pagelist to pages of myDoc
set pagetable to {}
repeat with pg in pagelist
set pgi to {pageID:id of pg, PageName:name of pg, PageLabel:label of pg}
copy pgi to the end of pagetable
end repeat
end tell
OK. But how transform this record to a xml tree? Without “manually” setting the xml node names? The node names must be a “variable” names from record (I’m looking for universal mechanism wchich transform a variable name from record to node name in xml).
In current example, the result xml will be:
214
23
Book
I have to build some XML. I ended up making a “template” of the XML structure I wanted, then inserted the proper values into the xml stream. Then outputted it as text (saved to a text file).
I use a loop to insert more copies of each node as needed.
What you’re building is not a “record” but a “list” containing records.
Records have “properties”.
Record properties have “labels” and “values”.
If the labels are all user defined, another easy way to convert them to text is:
set pagetable to {{pageID:214, PageName:23, PageLabel:"Book"}, {pageID:215, PageName:24, PageLabel:"Aardvark"}}
tell application "System Events"
make new property list item with properties {value:pagetable}
set {labelLists, valueLists} to {name, value} of property list items of property list items of result
end tell
labelLists --> {{"PageName", "pageID", "PageLabel"}, {"PageName", "pageID", "PageLabel"}}
valueLists --> {{23, 214, "Book"}, {24, 215, "Aardvark"}}
It would be nice to be able to use System Events’s XML Suite to create the XML text, but I’ve not been able to do anything with it.
FWIW, here’s another method using ASObjC Runner to do the work for you:
script listOfRecordsToXMLString
set theString to ""
-- passing parameter converted to array
set pagetable to current application's NSApp's convertedValue()
-- get count of array items
set theCount to pagetable's |count|()
-- loop through; zero-based indexes
repeat with i from 0 to (theCount as integer) - 1
set oneItem to pagetable's objectAtIndex_(i)
-- get all labels/keys
set theKeys to oneItem's allKeys()
repeat with oneKey in theKeys
-- make XML node and get its XML string
tell current application's NSXMLNode to set newText to elementWithName_stringValue_(oneKey, oneItem's valueForKey_(oneKey) as text)'s XMLString()
set theString to theString & newText & return
end repeat
end repeat
return theString
end script
set pagetable to {{pageID:214, PageName:23, PageLabel:"Book"}, {pageID:215, PageName:24, PageLabel:"Aardvark"}}
tell application "ASObjC Runner" to set theResult to run the script {listOfRecordsToXMLString} converting pagetable with result returned