Need Help getting information from System Profiler

Hi all, I am new to these forums and AppleScript in general. So far the language is pretty easy but I am having trouble with a certain task. I would like to have a script that pulls the Model Name and Model Identifier from the Mac I am on. I do not really understand how to go about doing this. Any help or resources would be greatly appreciated.

Hi,

easiest version


set {modelName, modelIdentifier} to paragraphs of (do shell script "system_profiler SPHardwareDataType | grep 'Model Name\\|Model Identifier' | cut -d ':' -f 2 | cut -b2-")

And here’s the long one. I created a routine that I use in various apps:

on getThisMachineSpecs()
	set thisMachineSpecs to {}
	set {thisModelName, thisModelIdent, thisModelCPUName, thisModelCPUSpeed, thisModelCPUNumber, thisModelCPUCores, thisModelRAM, thisModelBusSpeed, thisModelSerial, hardDriveSize, hardDriveFreeSpace, currentIpv4, currentOSVersion, currentShortName, currentLongUserName, currentBootVolume, currentComputerName, currentPrimEthernetAdd} to {"UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN"}
	
	try
		set scanOfSystemHardwareRaw to (do shell script "system_profiler SPHardwareDataType")
		
		set myText to every paragraph of scanOfSystemHardwareRaw
		
		set myOldDelimiters to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {": "}
		
		repeat with myCounter from 5 to (count of items in myText)
			set myLine to item myCounter of myText
			if myLine is "" then
			else
				set {kindData, theData} to text items of myLine
				--display dialog (kindData & return & theData)
				if kindData contains "Model Name" then set thisModelName to theData
				if kindData contains "Model Identifier" then set thisModelIdent to theData
				if kindData contains "Processor Name" then set thisModelCPUName to theData
				if kindData contains "Processor Speed" then set thisModelCPUSpeed to theData
				if kindData contains "Number Of Processors" then set thisModelCPUNumber to theData
				if kindData contains "Total Number Of Cores" then set thisModelCPUCores to theData
				if kindData contains "Memory" then set thisModelRAM to theData
				if kindData contains "Bus Speed" then set thisModelBusSpeed to theData
				if kindData contains "Serial Number" then set thisModelSerial to theData
			end if
		end repeat
		set AppleScript's text item delimiters to myOldDelimiters
	end try
	
	try
		tell (system info) to set currentIpv4 to get IPv4 address
	end try
	try
		tell (system info) to set currentOSVersion to get system version
	end try
	try
		tell (system info) to set currentShortName to get short user name
	end try
	try
		tell (system info) to set currentLongName to get long user name
	end try
	try
		tell (system info) to set currentBootVolume to get boot volume
	end try
	try
		tell (system info) to set currentComputerName to get computer name
	end try
	try
		tell (system info) to set currentPrimEthernetAdd to get primary Ethernet address
	end try
	
	
	if currentOSVersion begins with "10.4" then
		set currentOSVersion to ("Mac OS X Tiger " & currentOSVersion)
	else if currentOSVersion begins with "10.5" then
		set currentOSVersion to ("Mac OS X Leopard " & currentOSVersion)
	else if currentOSVersion begins with "10.6" then
		set currentOSVersion to ("Mac OS X Snow Leopard " & currentOSVersion)
	end if
	
	try
		tell application "System Events" to set HDSizeRaw to (get capacity of startup disk) as number
		set hardDriveSizeTemp to (my round_truncate((HDSizeRaw / 1.0E+9), 0))
		if hardDriveSizeTemp < 1 then set hardDriveSize to ((hardDriveSizeTemp * 1000) & " Mb")
		if hardDriveSizeTemp ≥ 1 then set hardDriveSize to (hardDriveSizeTemp & " Gb")
	end try
	
	try
		tell application "System Events" to set HDFreeSpaceRaw to (get free space of startup disk) as number
		set hardDriveFreeSpaceTemp to (my round_truncate((HDFreeSpaceRaw / 1.0E+9), 0))
		if hardDriveFreeSpaceTemp < 1 then set hardDriveFreeSpace to ((hardDriveFreeSpaceTemp * 1000) & " Mb")
		if hardDriveFreeSpaceTemp ≥ 1 then set hardDriveFreeSpace to (hardDriveFreeSpaceTemp & " Gb")
	end try
	
	set thisMachineSpecs to {thisModelName, thisModelIdent, thisModelCPUName, thisModelCPUSpeed, thisModelCPUNumber, thisModelCPUCores, thisModelRAM, thisModelBusSpeed, thisModelSerial, hardDriveSize, hardDriveFreeSpace, currentIpv4, currentOSVersion, currentShortName, currentLongName, currentBootVolume, currentComputerName, currentPrimEthernetAdd}
	return thisMachineSpecs
end getThisMachineSpecs

It’s refers to another routine that rounds numbers to a specified decimal, here it is (works on both french and english computers):

on round_truncate(this_number, decimal_places)
	set LocaleDecimal to "." as text
	
	try
		set testnumber to "0.5" as number
	on error
		set LocaleDecimal to "," as text
	end try
	
	if decimal_places is 0 then
		set this_number to this_number + 0.5
		return number_to_text(this_number div 1)
	end if
	
	set the rounding_value to "5"
	repeat decimal_places times
		set the rounding_value to "0" & the rounding_value
	end repeat
	set the rounding_value to (LocaleDecimal & the rounding_value) as number
	
	set this_number to this_number + rounding_value
	
	set the mod_value to "1"
	repeat decimal_places - 1 times
		set the mod_value to "0" & the mod_value
	end repeat
	set the mod_value to (LocaleDecimal & the mod_value) as number
	
	set second_part to (this_number mod 1) div the mod_value
	if the length of (the second_part as text) is less than the decimal_places then
		repeat decimal_places - (the length of (the second_part as text)) times
			set second_part to ("0" & second_part) as string
		end repeat
	end if
	
	set first_part to this_number div 1
	set first_part to number_to_text(first_part)
	set this_number to (first_part & LocaleDecimal & second_part) as real
	
	return this_number
end round_truncate

on number_to_text(this_number)
	set this_number to this_number as text
	if this_number contains "E+" then
		set x to the offset of "." in this_number
		set y to the offset of "+" in this_number
		set z to the offset of "E" in this_number
		set the decimal_adjust to characters (y - (length of this_number)) thru -1 of this_number as string as number
		if x is not 0 then
			set the first_part to characters 1 thru (x - 1) of this_number as string
		else
			set the first_part to ""
		end if
		set the second_part to characters (x + 1) thru (z - 1) of this_number as string
		set the converted_number to the first_part
		repeat with i from 1 to the decimal_adjust
			try
				set the converted_number to the converted_number & character i of the second_part
			on error
				set the converted_number to the converted_number & "0"
			end try
		end repeat
		return the converted_number
	else
		return this_number
	end if
end number_to_text

The only thing that’s not exactly right is how it reports on hard drive sizes and free space, I think my math is off, because it is reported as vendors-style (250 Gb hard disk) instead of logical (238 Gb).

Any one has an idea where i may be wrong?

Apple has changed the display of the sizes from binary (1024^x) to decimal(1000^x) prefixes in Snow Leopard

Have a look at NSDecimalNumber’s decimalNumberByRoundingAccordingToBehavior_ and initWithString_ methods. They should work in all locales.

I’ll sure do. This was pre-ASOC, had to manage somehow, the round command was a bit to simple for my needs…

So I should divide by 1024 instead of 1000 to get the correct size? At least to get the same thing as in the Finder… I’ll have to implement a if then to chek for pre-10.6 and post 10.6 too i guess…

Thanks all for the help. It worked out great.