查找使用 AppleScript 安装的 Mac OSX 版本

问题描述 投票:0回答:8

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

谢谢

macos applescript
8个回答
20
投票

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

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

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

单行:

set osver to system version of (system info)


18
投票

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

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

10
投票

我不在 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"

3
投票

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

tell application "Finder"
    set os_version to version
end tell

display dialog os_version

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


2
投票

我对 AppleScript 不太熟悉,但据我所知,您可以使用 sw_vers 命令从 shell 获取一些有关版本的信息。例如:

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

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


0
投票

尝试以下方法:

tell application "Terminal"
activate

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

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


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


0
投票

JXA(JavaScript)的@RSTM答案的更新版本:

function run() {
  const app = Application.currentApplication();
  app.includeStandardAdditions = true;
  return app.systemInfo().systemVersion;
}

标准版系统信息的更多字段,请参阅这个答案

© www.soinside.com 2019 - 2024. All rights reserved.