Monday, December 11, 2017

#1 2009-04-02 12:21:01 am

cirno
Member
Registered: 2005-05-30
Posts: 448

Model Identifier

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

Offline

 

#2 2009-04-02 01:48:31 am

Martin Michel
Administrator
From:: Berlin, Germany
Registered: 2008-03-03
Posts: 701
Website

Re: Model Identifier

How about this piece of code?

Applescript:


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


Plonum - Convert images to iWork Numbers documents

Offline

 

#3 2009-04-02 02:05:13 am

cirno
Member
Registered: 2005-05-30
Posts: 448

Re: Model Identifier

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

Applescript:

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

Offline

 

#4 2009-04-02 02:57:47 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Model Identifier

…or

Applescript:


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


regards

Stefan

Offline

 

#5 2017-11-30 10:27:42 am

antelop
Member
From:: New York, USA
Registered: 2017-11-28
Posts: 3

Re: Model Identifier

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

StefanK wrote:

…or

Applescript:


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




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. 

Applescript:

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)

Last edited by antelop (2017-11-30 10:47:25 am)


Best,
antelōp


Filed under: applescript, awk, system info

Offline

 

#6 2017-11-30 10:47:17 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4448

Re: Model Identifier

antelop wrote:

For the love of awk!


Hi. Welcome to MacScripter.

Applescript:


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

Enough sed!  wink


NG

Offline

 

#7 2017-11-30 10:48:07 am

antelop
Member
From:: New York, USA
Registered: 2017-11-28
Posts: 3

Re: Model Identifier

Nigel Garvey wrote:
antelop wrote:

For the love of awk!


Hi. Welcome to MacScripter.

Applescript:


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.

Last edited by antelop (2017-11-30 10:53:53 am)


Best,
antelōp

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)