【问题标题】:Application does not accept keystroke应用程序不接受击键
【发布时间】:2010-03-01 03:17:26
【问题描述】:

我正在尝试使用 AppleScript 向应用程序 VisualBoyAdvance 发送击键,但我无法让它工作。

到目前为止,我的代码是这样的:

tell application "VisualBoyAdvance"
    activate

    tell application "System Events"
        keystroke "k"
    end tell

end tell

当我直接告诉 VisualBoyAdvance 时,我得到了这个错误:

error "VisualBoyAdvance got an error: Can’t get keystroke \"k\"." number -1728 from keystroke "k"

我尝试过直接告诉 VisualBoyAdvance,我也尝试过使用key code 40,但我仍然无法让它工作。奇怪的是,这确实有效:

tell application "VisualBoyAdvance"
    activate

    tell application "System Events"
        keystroke "d" using {command down}
    end tell

end tell

但这是显示在菜单栏中的键盘快捷键,所以我想它会有点不同。

如何使用 AppleScript 模拟按键并让应用程序响应?如果我不能为此使用 AppleScript,我还能使用什么?

【问题讨论】:

  • 您是否开启了辅助设备支持?

标签: automation macros applescript


【解决方案1】:

我想你快到了。这是我用于 Safari 的东西;在本例中,我发送密钥代码 48(制表符)。

tell application "Safari"
    activate

    tell application "System Events" to tell process "Safari" to key code 48
end tell

AFAICS 这应该在很大程度上独立于目标进程中的 AppleScript 支持,因为您要求系统事件通过通用访问模拟按键。

有关关键代码的帮助,请参阅这个有用的应用程序:http://manytricks.com/keycodes/

【讨论】:

  • 还有:tell application "System Events" to tell process "Safari" to keystroke "d"
【解决方案2】:

让应用程序完全支持 Applescript 是开发人员的选择。从 Finder 的角度来看,菜单项是 Applescriptable 的,但其他 UI 选项可能是也可能不是。请参阅 UIElementInspector 以检查该应用程序是否有可编写脚本的元素。

【讨论】:

  • 还有其他方法可以在系统上模拟击键吗?
【解决方案3】:

我没有这个应用程序,所以我无法保证任何事情,但这里有一些东西可以尝试

 tell application "VisualBoyAdvance"
    activate
    tell application "System Events"
        tell application process "VisualBoyAdvance"
             try
            keystroke "k"
               on error
                  try
                    keystroke (ASCII character 75)
                   end try
               end try
        end tell
    end tell
 end tell

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    相关资源
    最近更新 更多