9

如何使用 AppleScript 找到我的 Mac 上安装的 OSX 版本?我想以编程方式安装应用程序并根据版本运行不同的 pkg 文件。

谢谢

4

7 回答 7

19

以下是如何使用内置函数在 AppleScript 中获取 OSX 版本:

将 sysinfo 设置为系统信息
将 osver 设置为 sysinfo 的系统版本

在 OS X Mavericks 上,结果是“10.9”。

单线:set osver to system version of (system info)

于 2011-07-25T01:28:09.713 回答
15

您可以使用以下命令将操作系统版本作为显示字符串获取:

set _versionString to system version of (system info)

如果您想将此与另一个版本进行比较,请务必使用considering numeric strings

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

否则,您可能会遇到诸如“10.4.11”小于“10.4.9”或“10.10”小于“10.9”等问题。

您也可以使用system attribute. 这使您可以将版本号作为整数获取,这样您就不必担心比较点分隔的字符串:

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

您还可以使用system attribute来获取各个版本组件,而无需解析字符串:

set _major to system attribute "sys1" -- 10
set _minor to system attribute "sys2" -- 9
set _bugFix to system attribute "sys3" -- 0
于 2013-10-25T17:26:39.423 回答
8

我不在 Mac 上,所以可能有更好的方法来做到这一点,但我想到的第一种方法就是执行一个 shell 命令来查询操作系统版本。

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

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

根据这些参考资料,您可能想要执行以下操作:

set os_version to do shell script "sw_vers -productVersion"
于 2009-01-31T05:36:51.603 回答
4

您也可以从 Finder 应用程序中获取版本

tell application "Finder"
    set os_version to version
end tell

display dialog os_version

在我的机器上显示“10.5.8”。

于 2009-06-09T04:32:53.353 回答
2

我对 AppleScript 不太熟悉,但是 AFAIK 你可以使用 sw_vers 命令从 shell 获取一些关于版本的信息。例如:

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

如果您可以从 appleScript 读取和解析它,那可能是一个解决方案,尽管我确信必须有更优雅的东西。

于 2009-01-31T05:36:38.937 回答
0

尝试以下方式:

tell application "Terminal"
activate

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

编辑:有人指出这确实会打开终端,这可能不是您想要的行为。

于 2009-01-31T05:51:47.703 回答
0

这对我有用

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

于 2015-03-04T11:55:45.123 回答