为什么 AppleScript 总是在按下 Command 的情况下发送击键?
我看到很多像这样的 AppleScript 示例
tell application "TextEdit"
activate
tell application "System Events"
keystroke "s"
end tell
end tell
预期结果是字母“s”将被键入到 TextEdit 中的活动文档中(假设至少有一个文档窗口)。但它总是尝试保存文档(是否保存已更改的文档并打开保存对话框(如果是新文档))。任何应用程序中的任何键在任何时间都会发生同样的事情...
有人知道为什么系统事件总是发送诸如“...使用{command down}”之类的击键吗?
I see a lot of examples of AppleScript like this
tell application "TextEdit"
activate
tell application "System Events"
keystroke "s"
end tell
end tell
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…
Does anybody know why System Events always send keystrokes like "… using {command down}"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用 Cmd+R 从 AppleScript 编辑器运行脚本,而不是通过单击“运行”按钮。在我按下“R”键后,脚本立即开始执行,并且该脚本在我释放 Cmd 或 R 之前发送击键“S”。这就是为什么发送的击键“S”由带有修饰符 Cmd 的 TextEdit 解释。
解决方法是单击“运行”按钮或在脚本开头添加延迟并使用 Cmd+R:
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.
The workaround is to click button Run or add delay at the beginning of script and use Cmd+R:
你如何执行脚本?如果您使用涉及 Command 键的键盘快捷键来执行此操作,那么您按住的是该键,而不是脚本。
How are you executing the script? If you're doing so with a keyboard shortcut involving the Command key, then you're holding down the key, not the script.
你确定吗?使用您发布的确切代码,它在 TextEdit 中输入了字母
s
。您仍然需要使用key down {command}
或key up {command}
来保存(⌘ Command S)。此外,如果在执行时按住 ⌘ Command 键,则在 TextEdit 中输入的命令将为 ⌘ Command S。
您可能需要查看 WikiBooks 上的这篇文章,这应该可以帮助您更好地理解它。
Are you sure? Using the exact code you posted, it typed the letter
s
in TextEdit. You'll still have to usekey down {command}
orkey up {command}
for it to do a save (⌘ Command S).Also, if you hold the ⌘ Command key down when executing, the command entered in TextEdit, would be ⌘ Command S.
You may want to look at this article on WikiBooks, which should help you understand it better.
最好检查一下,并仅在必要时应用延迟,并且只要有必要就应用延迟。
(~忙着等待用户执行 cmd up。我什至使用了一些令人不安的声音来促进它,因此脚本实际上以某种方式执行了 cmd up。:D)
并且还要特别注意,这种方式键序列不能变成命令,因为当 cmd 关闭时它不会触发。
注意:我使用了下载的工具。我还找到了 OsX 应该有的实用程序参考,但在 Lion 上没有运气。但下载的肯定可以用。
Even better to check for it, and apply the delay only if necessary, and as long as necessary.
(~Busy wait till user does cmd up. I even used some disturbing sound to facilitate it, so the script in fact does cmd up in a way. :D)
And also especially notice that this way the key sequence cannot turn into commands, because it won't fire while the cmd is down.
Notice: I used a tool I'd downloaded. I also found references of utilities OsX should have, but no luck on Lion. But the downloaded one works for sure.