XML Parsing [serial number and hostname]

In this code…

set mySysInfo to do shell script "/usr/sbin/system_profiler -xml"
tell application "System Events"
	set theInfo to XML element 3 of mySysInfo
        set theProps to properties of theInfo
end tell

theProps errors out with a “can’t get properties of theInfo”. If I log theInfo, it seems like it’s just the first char of mySysInfo. If I change the code to get XML element 3, theInfo gets the 3rd char of mySysInfo.

Has anyone seen this before?

TIA

Hi,

this cannot work, because the shell script returns just text
and the XML Suite of System Events must refer to a file on disk.

What information do you want to gather?

Thanks so much Stefan. Certainly I can pipe the shell’s output to a temp file and work with that. I’ve seen plenty of examples. I was just trying to skip the step, thinking it wasn’t neccessary.

Typically, the xml file is opened and then the “contents of theFileHandle” are read into a var. I assumed (incorrectly) that the type of the “contents of” was just a string. I wonder if there’s a way to coerce the string var I’ve already got to XML? Will investigate.

I just need to parse out the serial number and host name.

Thanks again.

You can get hostname from system info and just parse the hardware info for the SN


set hostName to host name of (system info)
set serialNum to word -1 of paragraph -2 of (do shell script "/usr/sbin/system_profiler SPHardwareDataType")

That doesn’t work if your hardware includes a Sudden Motion Sensor.

Edit: We’ve been over this before. :stuck_out_tongue:

Find serial number

Thanks for the :P. :slight_smile:

I did run through the system profiler search results and saw the alternative methods… but I am going to be using the system profiler. My question was really just about why my efforts to parse the xml results from a var weren’t working.

Thanks for your help!

The link was for everyone, but the actual :stuck_out_tongue: was more for Adam. :wink:

The System Events dictionary states that the contents property of an XML file returns XLM data.

You could start with something like this:

set xmlTempPath to POSIX path of ((path to temporary items folder as Unicode text) & "digest4d_system_profiler.xml")

do shell script "/usr/sbin/system_profiler -xml > " & quoted form of xmlTempPath

tell application "System Events"
	tell contents of XML file xmlTempPath
		-- whatever
	end tell
end tell

However, it might be easier to parse the result as a property list:

set xmlTempPath to POSIX path of ((path to temporary items folder as Unicode text) & "digest4d_system_profiler.xml")

do shell script "/usr/sbin/system_profiler -xml SPHardwareDataType SPSoftwareDataType > " & quoted form of xmlTempPath

tell application "System Events"
	tell contents of property list file xmlTempPath
		tell property list item 1 -- dict (for SPHardwareDataType)
			tell property list item 1 of property list item "_items" -- first dict of the _items array
				set serialNumber to value of property list item "serial_number"
			end tell
		end tell
	end tell
end tell

Are you looking for <key>local_host_name</key>? I ask because that isn’t the same value that would be returned by host name of (system info) (that value doesn’t seem to be in the system_profiler results). Both of those values are viewable in the Sharing pane of System Preferences, but when I think of hostname I think of the second value.

Thanks everyone! I’m good to go.