Creating xml tree from AppleScript record?

Hello everyone!

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:

But, how I do that?

Best wishes

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.

This is simple way.
But, in this case, I’m looking for universal transformer AS record->xml, without any text templates.

The name of the record = node name in xml
But, how get the name of item of the record? As simple text?

It is generally advised to use the names without resort to trickery; you know the record structure, so the names are known in advance.

If you must have a “universal” method:
This one use a temporary file to get them.
This one does the same via the clipboard.

This thread discusses some ins and outs. There are others, I’m sure.

Thank You.
Your post is very helpful, problem solved.


Just to get the terminology straight:

  1. What you’re building is not a “record” but a “list” containing records.
  2. Records have “properties”.
  3. 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. :confused:

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