{"id":35,"date":"2008-05-04T03:28:00","date_gmt":"2008-05-04T08:28:00","guid":{"rendered":"http:\/\/vgable.com\/blog\/2008\/05\/04\/getting-mac-os-x-version-information\/"},"modified":"2009-12-08T01:38:14","modified_gmt":"2009-12-08T06:38:14","slug":"getting-mac-os-x-version-information","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2008\/05\/04\/getting-mac-os-x-version-information\/","title":{"rendered":"Getting Mac OS X Version Information"},"content":{"rendered":"<h3>Cocoa<\/h3>\n<p>For a <strong>human-readable string<\/strong>, use <code>[[NSProcessInfo processInfo] operatingSystemVersionString]<\/code>.  It looks like &#8220;Version 10.5 (Build 9A581)&#8221;, but that format might change in the future.  NSProcessInfo.h explicitly warns against using it for parsing.  It will always be <em>human<\/em>-readable though.<\/p>\n<p>For <strong>machine-friendly numbers<\/strong>, use the <a href=\"http:\/\/developer.apple.com\/documentation\/Carbon\/Reference\/Gestalt_Manager\/Reference\/reference.html\">Gestalt<\/a> function, with one of the selectors:<br \/>\n<code>gestaltSystemVersionMajor<\/code> (in 10.4.17 this would be the decimal value 10)<br \/>\n<code>gestaltSystemVersionMinor<\/code> (in 10.4.17 this would be the decimal value 4)<br \/>\n<code>gestaltSystemVersionBugFix<\/code> (in 10.4.17 this would be the decimal value 17)<br \/>\n<strong>Do <em>not<\/em> use <a href=\"http:\/\/developer.apple.com\/documentation\/Carbon\/Reference\/Gestalt_Manager\/Reference\/reference.html#\/\/apple_ref\/doc\/c_ref\/gestaltSystemVersion\"><code>gestaltSystemVersion<\/code><\/a> unless your code needs to run on OS X 10.2 or earlier.<\/strong>  It&#8217;s a legacy function that can&#8217;t report minor\/bugfix versions > 9; meaning it can&#8217;t distinguish between 10.4.9 and 10.4.11.  The CocoaDev wiki has <a href=\"http:\/\/www.cocoadev.com\/index.pl?DeterminingOSVersion\">example code that works in 10.0<\/a> if you need to go that route.<\/p>\n<p>Here&#8217;s a simpler example of using Gestalt:<br \/><code><br \/>\n- (BOOL) usingLeopard {<br \/>\n&nbsp;&nbsp;long minorVersion, majorVersion;<br \/>\n&nbsp;&nbsp;Gestalt(gestaltSystemVersionMajor, &amp;majorVersion);<br \/>\n&nbsp;&nbsp;Gestalt(gestaltSystemVersionMinor, &amp;minorVersion);<br \/>\n&nbsp;&nbsp;return majorVersion  == 10 &amp;&amp; minorVersion == 5;<br \/>\n}<\/code><\/p>\n<h3>Scripts<\/h3>\n<p>For <strong>scripts and command-line work<\/strong>, there is the <a href=\"http:\/\/developer.apple.com\/DOCUMENTATION\/Darwin\/Reference\/ManPages\/man1\/sw_vers.1.html\">sw_vers<\/a> command. <code><br \/>\n$ sw_vers<br \/>\nProductName:\tMac OS X<br \/>\nProductVersion:\t10.5<br \/>\nBuildVersion:\t9A581<br \/>\n$ sw_vers -productName<br \/>\nMac OS X<br \/>\n$ sw_vers -productVersion<br \/>\n10.5<br \/>\n$ sw_vers -buildVersion<br \/>\n9A581<br \/>\n<\/code><\/p>\n<h3>Java<\/h3>\n<p>Check the value of:<\/p>\n<pre>System.getProperty(\"os.name\");\nSystem.getProperty(\"os.version\");\n<\/pre>\n<h3>Fallback<\/h3>\n<p>Finally, if you must, you can parse <code>\/System\/Library\/CoreServices\/SystemVersion.plist<\/code> &#8212; but  I don&#8217;t like the idea of doing that.  There&#8217;s a chance that the file could have been corrupted or maliciously altered.  It seems  safer, and less complicated, to directly ask the operating system it&#8217;s version.<\/p>\n<h3>Minimum OS Requirements<\/h3>\n<p>To enforce a minimum OS requirement for your application, you can set the <a href=\"http:\/\/developer.apple.com\/documentation\/MacOSX\/Conceptual\/BPRuntimeConfig\/Articles\/PListKeys.html#\/\/apple_ref\/doc\/uid\/20001431-113253\">LSMinimumSystemVersion<\/a> key in the <a href=\"http:\/\/developer.apple.com\/documentation\/MacOSX\/Conceptual\/BPRuntimeConfig\/Articles\/ConfigFiles.html\">Info.plist<\/a> file.  Big Nerd Ranch has a <a href=\"http:\/\/weblog.bignerdranch.com\/?p=13\">more robust, user-friendly, but complicated solution<\/a> for older legacy systems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cocoa For a human-readable string, use [[NSProcessInfo processInfo] operatingSystemVersionString]. It looks like &#8220;Version 10.5 (Build 9A581)&#8221;, but that format might change in the future. NSProcessInfo.h explicitly warns against using it for parsing. It will always be human-readable though. For machine-friendly numbers, use the Gestalt function, with one of the selectors: gestaltSystemVersionMajor (in 10.4.17 this would [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5,4,13,9],"tags":[366,279,544],"class_list":["post-35","post","type-post","status-publish","format-standard","hentry","category-macosx","category-objective-c","category-programming","category-sample-code","category-unix","tag-java","tag-mac-os-x","tag-scripting"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=35"}],"version-history":[{"count":2,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/35\/revisions"}],"predecessor-version":[{"id":522,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/35\/revisions\/522"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}