应用程序不接受击键

Jos*_*unt 5 macros applescript automation

我试图使用AppleScript向应用程序VisualBoyAdvance发送击键,但我无法让它工作.

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

tell application "VisualBoyAdvance"
    activate

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

end tell
Run Code Online (Sandbox Code Playgroud)

当我直接告诉VisualBoyAdvance时,我收到此错误:

error "VisualBoyAdvance got an error: Can’t get keystroke \"k\"." number -1728 from keystroke "k"
Run Code Online (Sandbox Code Playgroud)

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

tell application "VisualBoyAdvance"
    activate

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

end tell
Run Code Online (Sandbox Code Playgroud)

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

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

And*_*son 7

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

tell application "Safari"
    activate

    tell application "System Events" to tell process "Safari" to key code 48
end tell
Run Code Online (Sandbox Code Playgroud)

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

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


mar*_*dge 0

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