• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to work with XML data using System Events?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to work with XML data using System Events?


  • Subject: Re: How to work with XML data using System Events?
  • From: has <email@hidden>
  • Date: Fri, 4 Nov 2005 17:55:27 +0000

Matt Neuburg wrote:

> >How would I use these to read XML data from an XML file?
>
>Example (from you-know-where):
>
>tell application "System Profiler"
>    set s to (XML text of document 1)
>end tell
>on findElementWithValue(e, v)
>    tell application "System Events"
>        repeat with i from 1 to (count XML elements of e)
>            if value of XML element i of e is v then
>                return i
>            end if
>        end repeat
>    end tell
>    error "not found"
>end findElementWithValue
>tell application "System Events"
>    set x to make new XML data with data s
>    set e to XML element 1 of XML element 1 of x
>    set e to first XML element of e where ¬
>        value of XML element 2 of it is "SPHardwareDataType"
>    set i to my findElementWithValue(e, "_items")
>    set e to XML element 1 of XML element (i + 1) of e
>    set i to my findElementWithValue(e, "machine_name")
>    return value of XML element (i + 1) of e -- iMacG5
>end tell

Note that the above code makes an unsafe assumption about the location of the 'SPHardwareDataType' value, which is stored in a dictionary so should be looked up by key, not index. In theory you should be able to say:

	set e to first XML element of e where (value of XML element after (its first XML element whose value is "_dataType") is "SPHardwareDataType")

but the double test seems to be too much for SE to cope with.

But anyway, when working with plists it's simpler to use SE's dedicated PList Suite, e.g.:

-------

tell application "System Profiler"
	set s to (XML text of document 1)
end tell

tell application "System Events"
	set plist to make new property list item with data s
	set hardwareRec to first property list item of plist where the value of its property list item "_dataType" is "SPHardwareDataType" -- [1]
	set itemsList to property list item "_items" of hardwareRec
	set overviewRec to first property list item of itemsList where the value of its property list item "_name" is "hardware_overview"
	return value of property list item "machine_name" of overviewRec
end tell

-------

Unfortunately, the above code doesn't work when I tried it OMM (10.4.2) as the line marked '[1]' returns a reference to the wrong element. This appears to be a bug in System Events; don't know if it's fixed in 10.4.3, but if anyone can reproduce it there then they're welcome to file it. Still, routing around this problem:

-------

on findDictInListByValueOfItsKey(obj, key_, value_)
	tell application "System Events"
		set vals to value of property list item key_ of every property list item of obj
		repeat with i from 1 to count vals
			if item i of vals is value_ then return (a reference to property list item i of obj)
		end repeat
	end tell
	error "Not found."
end findDictInListByValueOfItsKey

tell application "System Profiler"
	set s to (XML text of document 1)
end tell

tell application "System Events"
	set plist to make new property list item with data s
	set hardwareRec to my findDictInListByValueOfItsKey(plist, "_dataType", "SPHardwareDataType")
	set itemsList to property list item "_items" of hardwareRec
	set overviewRec to my findDictInListByValueOfItsKey(itemsList, "_name", "hardware_overview")
	return value of property list item "machine_name" of overviewRec
end tell
--> "PowerBookG415"

-------

Not exactly elegant or concise, but it does the job.

HTH

has
--
http://freespace.virgin.net/hamish.sanderson/
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: How to work with XML data using System Events?
      • From: Paul Berkowitz <email@hidden>
  • Prev by Date: Re: Specific Date-Time Formatting
  • Next by Date: Re: Extract from result of current date
  • Previous by thread: Re: How to work with XML data using System Events?
  • Next by thread: Re: How to work with XML data using System Events?
  • Index(es):
    • Date
    • Thread