10

How can I find which version of OSX is installed on my Mac by using AppleScript? I want to install an app programatically and run different pkg files based on the version.

Thanks

7 Answers 7

19

Here is how to get OSX version in AppleScript, using built-in functions:

set sysinfo to system info
set osver to system version of sysinfo

On OS X Mavericks the result is "10.9".

One-liner: set osver to system version of (system info)

2
  • This seems to be the most "correct" way, as it relies completely on built-in functions
    – Jack James
    Oct 23, 2013 at 10:24
  • 1
    Any idea how this might be done in JXA (JavaScript for Automation)? Doesn't seem to have and SystemInfo object. May 21, 2015 at 16:29
16

You can get the OS version as a display string using:

set _versionString to system version of (system info)

If you want to compare this to another version, be sure to use considering numeric strings:

considering numeric strings
    set _newEnough to _versionString ≥ "10.9"
end considering

Otherwise, you can run into problems such as "10.4.11" being less than "10.4.9", or "10.10" being less than "10.9".

You can also use system attribute. This lets you get the version number as an integer so that you don't need to worry about comparing dot-separated strings:

set _versionInteger to system attribute "sysv" -- 4240 == 0x1090 (Mac OS X 10.9)
set _isMavericksOrBetter to (system attribute "sysv") ≥ 4240 -- 0x1090
set _isMountainLionOrBetter to (system attribute "sysv") ≥ 4224 -- 0x1080
set _isLionOrBetter to (system attribute "sysv") ≥ 4208 -- 0x1070

You can also use system attribute to get the individual version components without having to parse a string:

set _major to system attribute "sys1" -- 10
set _minor to system attribute "sys2" -- 9
set _bugFix to system attribute "sys3" -- 0
9

I'm not on a Mac, so there may be a better way to do this, but the first approach that comes to mind is just executing a shell command to query the OS version.

http://developer.apple.com/technotes/tn2002/tn2065.html#TNTAG2

http://developer.apple.com/DOCUMENTATION/Darwin/Reference/ManPages/man1/sw_vers.1.html

Based on these references, you probably want to do something like:

set os_version to do shell script "sw_vers -productVersion"
1
  • This is the perfect answer! Using "sw_vers -buildVersion" at the end, will give you the build version as well. Very helpful if you want to get more detailed information like. Check out this gist to see it in full action. Oct 18, 2022 at 9:49
4

You can get version from the Finder app as well

tell application "Finder"
    set os_version to version
end tell

display dialog os_version

On my machine this displays "10.5.8".

1
  • This returns the Finder version not the OS Version. In my case (6 years later =P) Finder version is 10.10.4 while os version is 10.10.3. May 21, 2015 at 16:22
2

I'm not too familiar with AppleScript, but AFAIK you can get some info about versions from the shell with the sw_vers command. For example:

Macintosh:~ udekel$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.5.6
BuildVersion:   9G55

If you can read and parse that from appleScript, that may be a solution, though I'm sure there has to be something more elegant.

0

Try something along these lines:

tell application "Terminal"
activate

set theVersion to do script with command "sw_vers -productVersion"
end tell

Edit : It was pointed out that this does open the terminal, and that probably isn't the behavior you want.

2
  • 3
    What if Terminal isn't running? This will launch it, scaring most users. Jan 31, 2009 at 6:13
  • 1
    That's true, however, I was assuming the poster would modify this code to fit their needs. Jan 31, 2009 at 6:18
0

This worked for me

set OSVersion to system version (system info)
if OSVersion as string < "10.9" or OSVersion as string > "10.9.5" then
- Add code to execute if condition met
else
- Add code to execute if condition not met
end if

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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