Model Identifier

I’m looking for a way to get Model Identifier. (iMac7,1)

How about this piece of code?


set macmodel to my getmacmodel()

on getmacmodel()
	set macmodel to missing value
	set command to "system_profiler SPHardwareDataType"
	set paras to paragraphs of (do shell script command)
	repeat with para in paras
		if para contains "Machine Model:" then
			set colonoffset to offset of ":" in para
			set macmodel to (characters (colonoffset + 2) through -1 of para) as text
			exit repeat
		end if
	end repeat
	return macmodel
end getmacmodel

Thanks, it works if i change “Machine Model” to “Model Identifier”.

set macmodel to my getmacmodel()

on getmacmodel()
	set macmodel to missing value
	set command to "system_profiler SPHardwareDataType"
	set paras to paragraphs of (do shell script command)
	repeat with para in paras
		if para contains "Model Identifier:" then
			set colonoffset to offset of ":" in para
			set macmodel to (characters (colonoffset + 2) through -1 of para) as text
			exit repeat
		end if
	end repeat
	return macmodel
end getmacmodel

.or


set macmodel to do shell script "/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Model Identifier/ {print $NF}'"

I know this is bumping a 10+ year old thread at this point, but hey, I like to ride dirty sometimes.

The {print $NF} only returns the very last “column” of text in awk, so if your are looking to capture “Mac mini” for Model Name, you only get “mini” returned.

The below (“awk -F” while teasing out the “:” delimiter) seemed to work a smidgen better for me, but I am still getting a leading space after parsing.

set macmodel to do shell script "/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk -F: '/Processor Speed/ {print $2}'"

$1 will return the text before the “:” delimiter; $2 will return the text after said delimiter, but how do I clean it up by removing any leading spaces? (is a gsub needed?)

Would sed be easier?

For the love of awk (or sed)!

Model: Mac mini
AppleScript: 2.7
Browser: Safari 604.3.5
Operating System: Mac OS X (10.13 Developer Beta 3)

Hi. Welcome to MacScripter.


set macmodel to (do shell script "/usr/sbin/system_profiler SPHardwareDataType  | sed -En '/Model Identifier:/ s/^[^:]+: *//p'")

Enough sed! :wink:

Ohhh! That is sweet, so she sed… :cool:

Thank you Nigel. This works beautifully for all fields returned from the system_profiler query.