In the Applescript library for System events there is a class titled “XML Suite” - tiger (10.4). Is there
is no docs on it anywhere? This is the only thing I can find on it:
Concerning XML, my suggestion is to use something else that Apple’s implementation that is buggy (a lot), but rather use the XMLLib osax from http://satimage-software.com
Browser: Safari 412
Operating System: Mac OS X (10.4)
Huh, I hadn’t seen this before. This seems to work:
set the_xml_file to (choose file) as Unicode text --choose an XML file
tell application "System Events"
set the_xml to (contents of XML file the_xml_file)
set the_elements to the_xml's XML elements
set the_name to the_elements's item 1's name
end tell
This does too:
set the_xml_file to (path to preferences folder as unicode text) & "com.apple.systemevents.plist"
tell application "System Events"
set the_xml to (contents of XML file the_xml_file)
set the_elements to the_xml's XML elements
set the_attributes to the_elements's item 1's XML attribute 1's properties
end tell
Ok I’ll be honest and admit Im having to get the applescript language guide out again… so apologies…
But how I do traverse the elements. e.g… carrying on from your wonderful (I mean that!) example:
on run
set the_xml_file to (choose file) as Unicode text --choose an XML file
--set XML file to XMLData
tell application "System Events"
set the_xml to (contents of XML file the_xml_file)
set the_elements to the_xml's XML elements
set numEles to (count the_elements)
set eleNum to 1
repeat numEles times
set the_curr_name to the_elements's item eleNum's name
log the_curr_name
display dialog the_curr_name
set eleNum to eleNum + 1
end repeat
end tell
end run
it’s wierd. it only seems to do the first element - e.g xml or rdf:rdf.
thanks for all your efforts! Id love to do this in normal 10.4 applescript - & save using 3rd party stuff!!
cheers
will
Model: G4 Powerbook
Browser: Safari 412
Operating System: Mac OS X (10.4)
The recursion took me a few minutes to work out but this should work for converting XML to an AppleScript record (it seemed to have problems if the XML had comments such as “”):
return my process_data(my get_data(), {})
-->
(*
{{XML element:"plist", xml_attributes:{{XML attribute:"version", value:"1.0"}}, xml_elements:{{XML element:"dict", xml_elements:{{XML element:"key", value:"AppleNavServices:ChooseFolder:0:HomeDirectoryPath"}, {XML element:"string", value:"file://~/Desktop/B/"}, {XML element:"key", value:"AppleNavServices:ChooseFolder:0:Path"}, {XML element:"string", value:"file://localhost/Macintosh%20HD/Users/jon/Desktop/B/"}, {XML element:"key", value:"AppleNavServices:ChooseFolder:0:Position"}, {XML element:"data", value:"
AOUBRg==
"}, {XML element:"key", value:"AppleNavServices:ChooseFolder:0:Size"}, {XML element:"data", value:"
AAAAAAFvAjA=
"}, {XML element:"key", value:"dv com.apple.soundmgr._DV Sound Output Settings"}, {XML element:"data", value:"
fQAAAA==
"}}}}}}
*)
on get_data()
--set the_xml_file to (choose file) as Unicode text --choose an XML file
set the_xml_file to ((path to preferences folder) as Unicode text) & "com.apple.systemevents.plist"
tell application "System Events" to return (contents of XML file the_xml_file)
end get_data
on process_data(the_xml, all_properties)
tell application "System Events"
repeat with e in my get_elements(the_xml)
set all_properties to my make_record(e, all_properties)
end repeat
return all_properties
end tell
end process_data
on make_record(e, p)
tell application "System Events"
set new_e to {XML element:my get_name(e)}
set a to {}
repeat with i in my get_attributes(e)
set end of a to {XML attribute:my get_name(i), value:my get_value(i)}
end repeat
if a ≠{} then set new_e to new_e & {xml_attributes:a}
try
set new_e to new_e & {value:my get_value(e)}
end try
set sub_e to {}
repeat with i in my get_elements(e)
set sub_e to sub_e & my make_record(i, {})
end repeat
if sub_e ≠{} then set new_e to new_e & {xml_elements:sub_e}
set end of p to new_e
return p
end tell
end make_record
on get_name(d)
try
tell application "System Events" to return d's name
on error
return ""
end try
end get_name
on get_value(d)
try
tell application "System Events" to return d's value
on error
return ""
end try
end get_value
on get_elements(d)
try
tell application "System Events" to return d's XML elements
on error
return {}
end try
end get_elements
on get_attributes(d)
try
tell application "System Events" to return d's XML attributes
on error
return {}
end try
end get_attributes
Jon
Model: PowerBook 1.25MHz
AppleScript: 1.10
Browser: Safari 412
Operating System: Mac OS X (10.4)
XML tool osax is great but is fine only for small files, wich are not very deep, its main disadvantage in my opinion is that you cannot manipulate or navigate in the XML file very easily. I first used this osax because it was able to make XSL transformations on a XML file but I’ve found many bugs in it (number formatin which are not related to the author of XML tools but rathers to the open source project he used). Since then, Satimage wrote XMLLib osax , far more intuitive to use than XML Tools or Apple’s parsing tools. Yes it resuiqres installing an other osax but it rocks :p. Go ahead make yourself your own opinion about which tool is the best for you. I’ve made my choice
Browser: Safari 412
Operating System: Mac OS X (10.4)
Jon, that is amazing. Not only the script - but the fact you calmly note it took just a few minutes. I feel sick
Thanks for that - a real learning experience. If your intrested to know what I was trying to do it was to geotag my images with lat/long codes by looking it up at http://brainoff.com/geocoder/ from place names. Sadly though I can’t edit exif data in iViewMedia Pro so my task has halted for now …
btw: Jean-Baptiste - this code is for the apple built-in xml parser to Tiger (10.4) - Not any 3rd party extension
thanks again.
will
Model: G4 Powerbook
Browser: Safari 412
Operating System: Mac OS X (10.4)
This would have taken only three lines with the XMLLib osax :
set theplist to PlistOpen alias (((path to preferences folder) as Unicode text) & "com.apple.systemevents.plist")
set theRecord to PlistGet theplist as record
PlistClose theplist
Browser: Safari 412
Operating System: Mac OS X (10.4)
If it’s a plist file you’re parsing, Tiger’s System Events has a Property List suite that isn’t as abstract as the XML suite. Getting a single value from a file is this easy:
tell application "System Events" to return the value of property list item "AlwaysShowTabBar" of property list file ((path of preferences folder) & "com.apple.Safari.plist")