为什么 AppleScript 总是在按下 Command 的情况下发送击键? [英] Why AppleScript always send keystrokes with Command down?

查看:16
本文介绍了为什么 AppleScript 总是在按下 Command 的情况下发送击键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到很多这样的 AppleScript 例子

I see a lot of examples of AppleScript like this

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

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

Expected result is that letter "s" will be typed into active document in TextEdit (assume there is at least one document window). But instead it always tries to save document (did it for changed document and open save dialog if it is a new). Same things happen for any key in any application at any time…

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

Does anybody know why System Events always send keystrokes like "… using {command down}"?

推荐答案

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

I run script from AppleScript Editor using Cmd+R, not by clicking Run button. Script begins executing immediately after I press key "R" down and that script sends keystroke "S" before I release Cmd or R. That's why sent keystroke "S" interprets by TextEdit with modifier Cmd.

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

The workaround is to click button Run or add delay at the beginning of script and use Cmd+R:

delay 0.2 -- 0.2 second delay is enough

tell application "TextEdit"
    activate

这篇关于为什么 AppleScript 总是在按下 Command 的情况下发送击键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆