How to script System Profiler via AS, NOT via do shell script?

Hi everyone,

I have some experimentation I’d like to try on the scriptability of System Profiler via vanilla AppleScript, as opposed to what information I can gather from the command line system_profiler app via do shell script.

I’ve looked through System Profiler’s dictionary, and it would appear that I can still gather reports based on detail level of basic/full/mini, but actually coding that continually results in any number of errors for me. Eventually, I’d like to be able to gather basic parameters (sadly, the generic “system info” only goes so far) such as AirPort MAC address, machine name, serial number, etc.

Would an enterprising coder with a few extra minutes mind trying to see which types of commands you can get to run from that? And also: Is there an easy way (osaxen or otherwise) in which, if I can gather an XML-based system profile in vanilla AppleScript, I can parse out the XML so as to gather specific key-value pairs from the generated profile? Again my goal is vanilla AppleScript, so for example I’m looking to avoid having to resort to a defaults command in a do shell script.

In any event: My thanks for the time, and I’ll look forward to seeing what (if anything) someone is able to come up with! :wink:

Best wishes,
MS

Is it OK if this only works on OS X v10.4?

I can help you with the XML part… Marc from Late Night Software has a free XML scripting addition at

http://www.latenightsw.com/freeware/XMLTools2/index.html

as for the rest I love the command line so thats where I usually resort to for a lot of things.

This should get the XML text:

tell application "System Profiler"
	launch
	tell front document
		set detail level to full
		set spXML to XML text
	end tell
	-- quit
end tell

I get a “System Profiler got an error: NSReceiverEvaluationScriptError: 4” error

Here’s the top of my result:

Model: Mac mini
AppleScript: 1.10.7
Browser: Safari 2.0.4 (419.3)
Operating System: Mac OS X (10.4)

and my system which its erroring on…

Model: MacBook Pro 17"
AppleScript: 1.10.7
Browser: Safari 2.0.4 (419.3)
Operating System: Mac OS X (10.4.7)

You can get either text or XML versions from system_profiler - see it’s man page

For XML

do shell script "/usr/sbin/system_profiler -xml"

To see usage without going to the man page:

try
	do shell script "/usr/sbin/system_profiler -usage"
on error E
	display dialog E
end try

system_profiler defaults to text.

Might want to read the subject again Adam. :wink:

This isn’t perfect, but it works for me:

tell application "System Profiler"
	launch
	tell front document
		set detail level to full
		set spXML to XML text
	end tell
	--quit
end tell

try
	set xmlFile to file specification ((path to temporary items as Unicode text) & "system_profile.xml")
	open for access (result) with write permission
	set fileRef to result
	
	write spXML to fileRef
	close access fileRef
on error errMsg number errNum
	try
		close access fileRef
	end try
	display alert "Error " & errNum message errMsg buttons {"Cancel"} default button 1
	error number -128
end try

set systemProfile to {airportMAC:missing value, machineName:missing value, machineSerial:missing value}

tell application "System Events"
	launch
	set xmlFile to XML file (POSIX path of xmlFile)
	
	tell XML element 1 of XML element "plist" of xmlFile -- plist/array
		tell XML element 1 of XML element 6 of XML element 1 -- plist/array/dict/array/dict
			set machineName of systemProfile to value of XML element 16
			set machineSerial of systemProfile to value of XML element 22
		end tell
		tell XML element 4 of XML element 2 of XML element 6 of XML element 2 -- plist/array/dict/array/dict/dict
			set airportMAC of systemProfile to value of XML element 2
		end tell
	end tell
end tell

return systemProfile

Oops, my bad… :rolleyes:, I got enthused mid-way through, forgot the topic title included: without.

Bruce, myndcraft, and Adam,

This is great!! I seem to have been getting hung up on the double-tell requirement of directing to the front document of System Profiler”I had been presuming that, like the command-line utility, that I could make a the request directly of the application itself (rather than of a document). As with most AS problems: It would help to think about what the app’s dictionary is showing me. :wink:

I’ll be tweaking this a bit for my needs over the next few days, so I’ll be sure to post my results for any that might be interested. Thanks much for the contributions, esp. to Bruce for the extensive code examples.

repeat 3 times
	say "MacScripter kicks butt!" using "Victoria"
end repeat

Best wishes,
MBJ

Glad we could help. :slight_smile: