24

There are several terminal commands to retrieve the numerical / abbreviated Model Identifier for Mac hardware.

  • sysctl hw.model
  • system_profiler SPHardwareDataType

These dump a string with the hardware name and x,y versioning. Currently we have to maintain a table to convert these identifiers into the full english machine description.

Is there an API, or better a command line tool that can produce the more human friendly names that match Apple's documentation MacBook Pro 15-inch, Mid 2009 or MacBook Pro (13-inch, 2016, Four Thunderbolt 3 Ports) instead of the more abbreviated MacBookPro5,3 or MacBookPro13,2 Model Identifier?

MacBookPro5,3 MacBookPro13,2

To summarize, how does System Profiler get the full localized names of Macintosh hardware in a way I can generate to consume that information systematically via script or program?

0

4 Answers 4

16

Did check the network traffic. System Information is connecting to

http://support-sp.apple.com

A full query looks like:

http://support-sp.apple.com/sp/product?cc=DJWR&lang=de_DE

Where 'DJWR' are the last four characters of the serial number

More info here: http://blog.coriolis.ch/get-your-apple-device-model-name-in-a-readable-format/

3
  • Wow - now that is a cool API. I would comment that this API does seem to expose some shift in serial numbers by Apple. Either the last 3 characters or the last 4 characters are to be sent. In my case, my iPad (TZ39) and MacBook Pro (J64B) cause errors, but work when I send the last three. I seem to recall hearing that the serial number encoding scheme changed to differentiate between 2009 / 2010 / 2011 manufacture earlier in the string, but I'll just use this API to send 4 and then re-try with 3 if I get an error. Great answer and alternate to the PrivateFramework plist.
    – bmike
    Nov 22, 2011 at 15:27
  • This works the best and the system_profiler -xml SPHardwareDataType command from JWWalker lets me get the serial number programmatically to then parse and feed into the API.
    – bmike
    Dec 11, 2014 at 20:02
  • We are using the same API and getting this error "<?xml version="1.0" encoding="utf-8" ?><root><error>0009</error><locale>en_US</locale></root>" Just want to check whether Apple supports this api . are there any alternatives ?
    – Atul Kumar
    Jul 26, 2018 at 9:29
14

ServerKit.framework has a property list within its resources that can help you with model identifier <-> model name string translation:

/System/Library/PrivateFrameworks/ServerKit.framework/Versions/A/Resources/English.lproj/XSMachineAttributes.plist

3
  • 4
    In new version of SDK, it's in /System/Library/PrivateFrameworks/ServerInformation.framework/Versions/A/Resources/English.lproj/SIMachineAttributes.plist
    – Itachi
    May 13, 2013 at 9:42
  • @Itachi Even that file is out of date. For example, my 2013 Model Retina MBP is MacBookPro10,2 and the file lists it as 2012 and not 2013. Perhaps the system calls the API below once and caches the result effectively forever?
    – bmike
    May 29, 2013 at 15:42
  • MacBookPro10,2 is made in late 2012 according to the plist dictionary, maybe you just bought it in 2013. Actually, the products 2013 haven't been released until Apple WWDC 2013, as far as I know.
    – Itachi
    May 31, 2013 at 5:22
7

No need for web requests to get this info.

The current model, as it appears in About This Mac, can be gathered from:

~/Library/Preferences/com.apple.SystemProfiler.plist

The plist contains names localized in the current users added languages:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>GTF1-sv-SE_SE</key>
    <string>MacBook Pro (15 tum, 2016)</string>
    <key>GTF1-en-SE_SE</key>
    <string>MacBook Pro (15-inch, 2016)</string>
</dict>
</plist>

Where the keys are simply the last 4 letters of the serial number (GTF1) followed by locale.

If you want to, you can even edit this plist to customise your About This Mac window, a common practice amongst Hackintosh users.

EDIT:

Full implementation in Swift 5:

public enum SystemInfo {

    public static var serialNumber: String? {
        let service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"))
        return IORegistryEntryCreateCFProperty(service, "IOPlatformSerialNumber" as CFString, kCFAllocatorDefault, 0).takeUnretainedValue() as? String
    }

    public static var modelName: String? {
        guard let serial = serialNumber,
            let plist = try? PropertyList.load(from: .init(fileURLWithPath: "\(NSHomeDirectory())/Library/Preferences/com.apple.SystemProfiler.plist")),
            let regionCode = Locale.current.regionCode,
            let names = plist["CPU Names"] as? [String: String],
            !names.isEmpty else {
                return nil
        }
        for language in Locale.preferredLanguages {
            let key = "\(serial.suffix(4))-\(language)_\(regionCode)"
            if let entry = names[key] {
                return entry
            }
        }
        return nil
    }

}

Where I'm also using this helper enum:

public enum PropertyList {

    public static func load(from url: URL) throws -> [String: Any]? {
        guard let plist = FileManager.default.contents(atPath: url.path) else { return nil }
        var format = PropertyListSerialization.PropertyListFormat.xml
        return try PropertyListSerialization.propertyList(from: plist, options: .mutableContainersAndLeaves, format: &format) as? [String: Any]
    }

    public static func save(_ plist: [String: Any], to url: URL) throws {
        let data = try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0)
        try data.write(to: url, options: .atomic)
    }

}

Usage:

print(SystemInfo.modelName)

Returns (Swedish):

MacBook Pro (15 tum, 2016)
4
  • This is awesome. Do you know how to pick apart the correct entry for CPU_Names. On a MBP I checked, there are three wrong names and one correct.
    – bmike
    Sep 6, 2019 at 11:28
  • I see ` "G8WP-en_US" => "MacBook Pro (Retina, 15-inch, Mid 2015)" "G8WP-en-US_US" => "MacBook Pro (Retina, 15-inch, Mid 2015)" "GTJ8-en-US_US" => "MacBook (Retina, 12-inch, Early 2016)" "HF1P-en-US_US" => "MacBook Pro (13-inch, 2016, Four Thunderbolt 3 Ports)" ` for the machine edited in to the question just now after your excellent post.
    – bmike
    Sep 6, 2019 at 11:39
  • 1
    The keys are simply the last 4 letters of the Mac serial #, followed by locale. You can get the serial number from IOKit: github.com/0infinity/IOPlatformSerialNumber/blob/master/cpuid.c Updated my answer.
    – Oskar
    Sep 6, 2019 at 11:41
  • Added full implementation
    – Oskar
    Sep 6, 2019 at 14:47
5

You could use system_profiler -xml SPHardwareDataType and look for the machine_name key.

EDIT: Granted, this doesn't answer the question in the title of how the System Profiler does it, but it provides a way to do it in your own code.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.