我看到很多这样的 AppleScript 例子

tell application "TextEdit"
    activate
    tell application "System Events"
        keystroke "s"
    end tell
end tell

预期的结果是字母“s”将被输入到 TextEdit 中的事件文档中(假设至少有一个文档窗口)。但相反,它总是尝试保存文档(为更改的文档执行此操作,如果是新文档则打开保存对话框)。任何时候任何应用程序中的任何键都会发生同样的事情......

有谁知道为什么系统事件总是发送诸如“... using {command down}”之类的击键?

最佳答案

我使用 Cmd+R 从 AppleScript Editor 运行脚本,而不是通过单击运行按钮。脚本在我按下“R”键后立即开始执行,并且该脚本在我释放 Cmd 或 R 之前发送击键“S”。这就是为什么发送的击键“S”由带有修饰符 Cmd 的 TextEdit 解释。

解决方法是单击按钮运行或在脚本开头添加延迟并使用 Cmd+R:

delay 0.2 -- 0.2 second delay is enough

tell application "TextEdit"
    activate

关于applescript - 为什么 AppleScript 总是在按下 Command 的情况下发送击键?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6584357/

10-15 03:21