How can I determine the running Mac OS X version programmatically?
Asked Answered
J

8

14

I have a program which needs to behave slightly differently on Tiger than on Leopard. Does anybody know of a system call which will allow me to accurately determine which version of Mac OS X I am running. I have found a number of macro definitions to determine the OS of the build machine, but nothing really good to determine the OS of the running machine.

Thanks, Joe

Jigsaw answered 1/10, 2008 at 14:6 Comment(1)
possible duplicate of Mac OS X: Replacement for Gestalt() for testing OS version at runtimeRoadrunner
W
15

See this article here

But in short, if you're using carbon, use the Gestalt() call, and if you're using cocoa, there is a constant called NSAppKitVersionNumber which you can simply check against.

Edit: For Mac OSX 10.8 and above, don't use Gestalt() anymore. See this answer for more details: How do I determine the OS version at runtime in OS X or iOS (without using Gestalt)?

Watchword answered 1/10, 2008 at 14:13 Comment(3)
as described in the linked article, checking against NSAppKitVersionNumber isn't so good as it tells you the version of AppKit, not the version of Mac OS X. The two are not linearly correlated.Dessau
You should still be able to call Gestalt() from Obj-C code, though.Watchword
The Gestalt() call has been deprecated since 10.8 and is no longer advisable for use. See #11073304Raynata
D
13

Could you just check for the presence of a capability? For instance:

if (NSClassFromString(@"NSKeyedArchiver") != Nil)

or

if ([arrayController respondsToSelector: @selector(selectedIndexes)])

then you know that the operating system does what you need it to do, not that Apple's product marketing group gave it a particular number ;-)

Dessau answered 1/10, 2008 at 21:52 Comment(2)
For most situations this is probably better. Thanks :)Eraeradiate
If you are using something specific that might not be present on the system, this is a very good idea and is something Apple has always recommended. If you are making a project wide policy decision to support only a specific minimum OS version and you have testers who are testing on that version and greater, then you should also check the OS version. One easy way to do this is by adding the LSMinimumSystemVersion key in your Info.plist file.Paillette
M
8

The API is through the Gestalt Manager.

See "Determining OS Version" in the CocoaDev site.

Mcgowan answered 1/10, 2008 at 14:15 Comment(0)
S
6

In terminal:

system_profiler SPSoftwareDataType

Gives:

Software:

    System Software Overview:

      System Version: Mac OS X 10.5.5 (9F33)
      Kernel Version: Darwin 9.5.0
      Boot Volume: Main
      Boot Mode: Normal
      Computer Name: phoenix
      User Name: Douglas F Shearer (dougal)
      Time since boot: 2 days 16:55

Or:

sw_vers

Gives:

ProductName:    Mac OS X
ProductVersion: 10.5.5
BuildVersion:   9F33
Sulfanilamide answered 1/10, 2008 at 14:10 Comment(1)
And from that I get: sw_vers | awk '/ProductVersion/ {print $2}' Very helpful. Thanks.Heather
P
2

Is the OS version really what you want? There may be a more appropriate thing to test for, such as the presence of, or version number of, a particular framework.

Provided answered 1/10, 2008 at 15:19 Comment(1)
One of the reason's I am interested in determining which OS version is that support for hardware stereo visualization stopped functioning properly in Leopard, but continues to work in Tiger. Both versions indicate support for it, but only Tiger does it correctly.Jigsaw
M
1

within your program you can use Gestalt. Here is the code I am using for my program to obtain the OS version.

long version = 0;
OSStatus rc0 = Gestalt(gestaltSystemVersion, &version);
if((rc0 == 0) && (version >= 0x1039)) {      
    // will work with version 10.3.9
    // works best with version 10.4.9
    return; // version is good
}
if(rc0) {
    printf("gestalt rc=%i\n", (int)rc0);
} else {
    printf("gestalt version=%08x\n", version);
}
Mohave answered 28/7, 2009 at 8:7 Comment(2)
Better to use the separate version-number-component selectors, just in case Apple ever goes over 0xf in either the minor or bug-fix nibble. (They've already gotten up to 0xb in the Tiger series.)Yakutsk
Very good point. gestaltSystemVersionMajor and gestaltSystemVersionMinor. Thank you. will use.Mohave
M
0

respondsToSelector: almost certainly is better than you maintaining a table of what given releases do and do not implement.

Be lazy. Let the runtime tell you whether it can do something or not, and fall back to older methods when you need to. Your code will be far less fragile because you don't have to maintain your own global data that the rest of your code has to keep checking with.

Maximilian answered 21/11, 2008 at 21:29 Comment(0)
P
0

Run this in the command line:

system_profiler SPSoftwareDataType | grep Mac
Patti answered 16/3, 2009 at 20:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.