macOS API for hardware model name?

If I go to "System Settings" -> "General" -> "About", it says "MacBook Air" and below that "M2, 2022"

How can I get these strings programatically? The following C code gets me

#include <stdio.h>
#include <sys/sysctl.h>
int main() {
    static const unsigned namelen = 2;
    int name[namelen] = {CTL_HW, HW_PRODUCT};
    char buffer[256];
    size_t bufferSize = sizeof(buffer);
    if (0 != sysctl(name, namelen, buffer, &bufferSize, NULL, 0)) {
        perror("sysctl");
        return 1;
    }
    printf("%s\n", buffer);
}

the string "Mac14,2", which is the hardware model identifier. But I want to get the user-friendly model name, e.g. "MacBook Air (13-inch, M2, 2022)". How can I do this?

Answered by DTS Engineer in 812599022

The technique suggested there are unsupported.

Using random properties you find in I/O registry is not something we support. The supported properties are those with symbolic constants in IOKit framework.

Similarly, classes that start with Apple are not considered API in I/O Kit.

And similar for undocumented property lists lurking deep in Apple frameworks.

Now, this won’t stop people from using this stuff, but it’s not the path to long-term binary compatibility. I regularly see threads here on DevForums from folks who’ve ignored these rules and found that their software no longer works with a new OS release or new hardware.

Regardless of what else you do here, please file an enhancement request for an API to get the info you need (and post your bug number, just for the record).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I believe that you need your own lookup table. (That's certainly the case on iOS; I'm less familiar with MacOS.)

Okay, Apple, can I make this a feature request?

Or alternatively, provide public access to a lookup table? How am I to deal with new models being released?

can I make this a feature request?

Of course you can! You know about Feedback Assistant, right?

How am I to deal with new models being released?

By updating your table!

By updating your table!

I end up poking around several different Wikipedia pages to get this information AFTER the new machines have shipped. And then I have to release a new version of my software to all of my customers (this is non-trivial)!

Apple will update its series of "Identify your ..." support articles once the devices ship. Those will provide Apple's official names, model identifiers, tech support links, etc. Localized versions will follow in a few days.

Hello. Thanks for asking. You can get some of this information from the ProcessInfo API.

Hello. Thanks for asking. You can get some of this information from the ProcessInfo API.

No. [[NSProcessInfo processInfo] hostName] is NOT what I want.

I'm not sure if this counts as getting it "programmatically", but you can get this information using the command line tool system_profiler. (You can run a command line tool programmatically using the Swift class Process or the Objective-C class NSTask.) For instance if you parse the output of the command system_profiler -detailLevel mini -json and look for the key SPHardwareDataType, and within that machine_name, you'll find the friendly name. This will be the first part, like "MacBook Pro". The less friendly model name is under the key path SPHardwareDataType.machine_model. But I don't know how to get the user-friendly second part like "15-inch, 2018".

JWWalker, that's great!

But, yes, I still want the user-friendlly second part. "15-inch, 2018" or "M2, 2022"

The technique suggested there are unsupported.

Using random properties you find in I/O registry is not something we support. The supported properties are those with symbolic constants in IOKit framework.

Similarly, classes that start with Apple are not considered API in I/O Kit.

And similar for undocumented property lists lurking deep in Apple frameworks.

Now, this won’t stop people from using this stuff, but it’s not the path to long-term binary compatibility. I regularly see threads here on DevForums from folks who’ve ignored these rules and found that their software no longer works with a new OS release or new hardware.

Regardless of what else you do here, please file an enhancement request for an API to get the info you need (and post your bug number, just for the record).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

please file an enhancement request

Done: Feedback ID is FB15654919 .

macOS API for hardware model name?
 
 
Q