I just got a report that one of my AppleScripts was returning the wrong version number on OS 10.9.5. I am running OS 10.9.5 but when I run this snippet of AppleScript:
set OSXversion to ""
tell application "Finder"
set OSXversion to version
end tell
OSXversion
Sure enough, what is returned is the string: “10.9.4” . Is this old news? Does Apple know about this? Anything smarter that can be done other than waiting for Apple to fix it?
Thanks in advance for any wisdom you’all might have.
I guess that one only gives you the product version of finder.
Now, this is a really fast way to get the os version with major and minor revision. I guess there are better and still faster ways to do this, by AsObjC, but this should work for starters.
set os_version to do shell script "sw_vers -productVersion"
I just want to point out, that at least on my machine, the do shell script way of getting the operating system version, is considerably faster. Maybe because System info get so much information in one go.
‘system info’ is much the faster on my machine ” although, since a script should only need to get the information once per run, it doesn’t really matter.
Faster still though (;)) is:
set sys_ver to ((system attribute "sys1") as text) & "." & (system attribute "sys2") & "." & (system attribute "sys3")
The oldest method I know gives even this a run for its money, though I don’t know how it copes with OS 10.10.0:
tell (system attribute "sysv") to set sys_ver to ((it div 4096) as text) & it mod 4096 div 256 & "." & it mod 256 div 16 & "." & it mod 16
If the values of the minor or bug fix revision are larger than 9, then gestaltSystemVersion will substitute the value 9 for them. For example, when running OS X v10.3.15, this selector returns 0x1039. When running OS X v10.10.5, this selector returns 0x1095.
Thanks Stefan. A similar thing happened with Mac OS 10.4.10 and 10.4.11, both of which the “sysv” method rendered as 10.4.9. “sys1”, “sys2”, and “sys3” were invented at that time for that very reason, although the corresponding “ascv” correctly rendered an AppleScript subversion which went into double figures.
Although I said above that ‘system attribute “sysv”’ was the oldest method I knew, it is in fact a later manifestation of the method! The ‘system attribute’ event, now in the StandardAdditions, used to be a Finder command called ‘computer’. During the years when they overlapped (somewhere around Mac OS 9.0, I think), the token would compile as either ‘computer’ or ‘system attribute’ depending on the system itself. It still does:
tell app "Finder" to tell (computer "sysv") to set sys_ver to ((it div 4096) as text) & it mod 4096 div 256 & "." & it mod 256 div 16 & "." & it mod 16
FWIW, Gestalt, which is what system attribute is based on, has been deprecated since 10.8, and the admonitions to use different methods seem to have become more strident of late. I wouldn’t be surprised if system info used the same thing under the hood, but at least that’s something the AppleScript team can change.
Unfortunately the official suggested replacements will only work for scripters under recent versions of the OS, which reduces their value more than somewhat.
Deprecated is a strange term. The best definition I can find is this:
On the basis of the headers and what’s been said elsewhere, I suspect Gestalt is under more threat than some other stuff: the replacement is being heavily promoted. But it’s quite possible that a lot of system info stuff comes from Gestalt too. The difference, though, is that system info could be (has been?) updated to newer APIs without breaking existing scripts, whereas system attribute is pretty much hard-wired into Gestalt.
Of course if system attribute does break, that in itself will be something to flag the OS version…
You’ve no doubt read my cynical comments in the past about the method for finding out the system version depending on the system version.
system attribute isn’t shown as deprecated in 10.9’s StandardAdditions dictionary either, but presumably you’re getting your information from developer documentation.
try
tell application "Finder" to tell (computer "sysv") to set sys_ver to ((it div 4096) as text) & it mod 4096 div 256 & "." & it mod 256 div 16 & "." & it mod 16
if (sys_ver contains ".9") then
try
set sys_ver to ((system attribute "sys1") as text) & "." & (system attribute "sys2") & "." & (system attribute "sys3")
end try
end if
on error
try
set sys_ver to system version of (system info)
on error
try
set sys_ver to (do shell script "sw_vers -productVersion")
on error
error "Your operating system hasn't been invented yet."
end try
end try
end try
There are (too) many ways to skin this cat. If you know the kernel version you could also subtract the number by 4. A kernel version 12.3 runs Mac OS X 10.8.3.
Another way is using a do shell script with system_profiler:
do shell script "system_profiler SPSoftwareDataType | egrep -o '10\\.[0-9]{1,2}\\.[0-9]{1,2}'"
I believe I have. But I think your cynicism has been trumped: the use AppleScript version statement eliminates the problem by doing away with the ability to get to first base with older versions of the OS.
Sorry, yes. If you look up Gestalt.h, it becomes fairly obvious that system attribute is just a wrapper around the Gestalt() function. The function itself is marked as deprecated, and many of the four-letter enums are simply obsolete. But the version-related enums get their own deprecation warning as well, telling developers: “Use NSProcessInfo’s operatingSystemVersion property instead.” And of course that property is only available in 10.10
Maybe I’m reading too much into it, and maybe it’s getting special treatment because it’s one of the few cases where there hasn’t been a great alternative. But they seem to be laboring the point more than usual. (Reading Apple documentation is sometimes a bit like reading tea leaves.)
So an all OS version solution would be reading the SystemVersion.plist file then?
set systemInfoFile to POSIX file "/System/Library/CoreServices/SystemVersion.plist" as string
tell application "System Events" to return value of property list item "ProductUserVisibleVersion" of property list file systemInfoFile