应用程序不接受击键

时间:2010-03-01 03:17:26

标签: automation macros applescript

我正在尝试使用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,我还能使用其他什么?

3 个答案:

答案 0 :(得分:7)

我想你差不多了。这是我用于Safari的东西;在这个例子中,我发送密钥代码48(标签)。

tell application "Safari"
    activate

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

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

有关密钥代码的帮助,请参阅此有用的应用程序:http://manytricks.com/keycodes/

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我不能保证任何东西,因为我没有这个应用程序,但这里有一些尝试

 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