为什么 AppleScript 总是在按下 Command 的情况下发送击键?

发布于 2024-11-18 07:59:07 字数 355 浏览 1 评论 0原文

我看到很多像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鹤舞 2024-11-25 07:59:07

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

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

delay 0.2 -- 0.2 second delay is enough

tell application "TextEdit"
    activate

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:

delay 0.2 -- 0.2 second delay is enough

tell application "TextEdit"
    activate
独享拥抱 2024-11-25 07:59:07

你如何执行脚本?如果您使用涉及 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.

淡淡的优雅 2024-11-25 07:59:07

你确定吗?使用您发布的确切代码,它在 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 use key down {command} or key 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.

眸中客 2024-11-25 07:59:07

最好检查一下,并仅在必要时应用延迟,并且只要有必要就应用延迟。
(~忙着等待用户执行 cmd up。我什至使用了一些令人不安的声音来促进它,因此脚本实际上以某种方式执行了 cmd up。:D)

并且还要特别注意,这种方式键序列不能变成命令,因为当 cmd 关闭时它不会触发。

on check()
    do shell script "~/Documents/checkModifierKeys cmd" --DOWNLOAD: http://macscripter.net/viewtopic.php?pid=114479#p114479
end check

on run {input, parameters}
    set the date_stamp to do shell script "date '+%Y.%m.%d_%H:%M'"
    repeat while check() = "1"
        beep
        delay 0.2
    end repeat
    tell application "System Events"
        tell process "TextEdit" to keystroke date_stamp
    end tell

    return input
end run

注意:我使用了下载的工具。我还找到了 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.

on check()
    do shell script "~/Documents/checkModifierKeys cmd" --DOWNLOAD: http://macscripter.net/viewtopic.php?pid=114479#p114479
end check

on run {input, parameters}
    set the date_stamp to do shell script "date '+%Y.%m.%d_%H:%M'"
    repeat while check() = "1"
        beep
        delay 0.2
    end repeat
    tell application "System Events"
        tell process "TextEdit" to keystroke date_stamp
    end tell

    return input
end run

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文