用于显示对话框的 AppleScript "with icon stop" 在 MacOS 10.12 上不工作

AppleScript "with icon stop" for display dialog not working on MacOS 10.12

在 MacOS 10.5 上,此 osascript 命令按预期工作,显示带有红色停止符号的对话框:

osascript -e 'tell app "System Events" to activate' 
          -e 'tell app "System Events" to display dialog "Picking a folder failed, exiting." with title "Start Up Error" buttons "Ok" with icon stop default button "Ok"' 
          -e 'tell app "System Events" to quit'

一个 MacOS 10.12,但是,此命令失败并显示以下神秘错误消息:

427:433: syntax error: Expected end of line, etc. but found class name. (-2741)

我发现把上面的图标"stop"改成"note"在10.12上可以正常工作,显示对话框没有错误,例如:

osascript -e 'tell app "System Events" to activate' 
          -e 'tell app "System Events" to display dialog "Picking a folder failed, exiting." with title "Start Up Error" buttons "Ok" with icon note default button "Ok"' 
          -e 'tell app "System Events" to quit'

其他人可以重现这个问题吗?除了避免停止图标之外,有没有办法解决它?我猜这是 AppleScript 中的一个错误,但也许我遗漏了什么。

原因是术语与 System Events 冲突。请改用 Finder

osascript -e 'activate application "Finder"'
          -e 'tell application "Finder" to display dialog "Picking a folder failed, exiting." with title "Start Up Error" buttons "Ok" with icon stop default button "Ok"'

由于 Finder 是 运行 永久的,因此可以省略 quit 行。

一般来说,如果您希望针对任何出现此类术语冲突的 AppleScriptable 应用程序,通常也可以首先将冲突术语分配给外部范围内的变量,然后再在 tell scope 改用那个变量。

例如,如果 System Events 应用与 stop 字词(来自 StandardAdditions.osax)发生冲突,您可以执行以下操作:

osascript -e 'set kStopIcon to stop' \
          -e 'tell application "System Events" to display dialog "Picking a folder failed, exiting." with title "Start Up Error" buttons {"Ok"} with icon kStopIcon default button "Ok"'

{还进行了一些其他或多或少的迂腐调整:现在有一个明确的行继续(\newline),以便于测试在 shell;并且,buttons … 标记参数现在直接指定一个列表,而不是涉及隐式强制转换。}