首页 > 解决方案 > macOS 定期向活动应用发送击键

问题描述

只要DBeaver 是活动应用程序,我正尝试每分钟向名为“Dbeaver”的 macOS (Mojave) 应用程序发送一次击键(command+ shift+ r) 。我试过以下没有效果。

tell application "System Events"
    set activeApp to name of first application process whose frontmost is true
    if "DBeaver" is in activeApp then
        tell application "System Events" to keystroke "r" using {command down, shift down}

    end if
end tell

如果它像下面这样简单,则该脚本可以正常工作:

activate application "DBeaver" 
tell application "System Events" to keystroke "r" using {command down, shift down}

标签: macosapplescriptmacos-mojaveosascript

解决方案


我没有您提到的应用程序,但我使用 TextEdit.app 测试了以下 AppleScript 代码并且它有效。如果您遇到任何错误或问题,请告诉我

tell application "System Events"
    repeat while (exists of application process "DBeaver")
        set activeApp to name of first application process whose frontmost is true
        if "DBeaver" is in activeApp then
            tell its application process "DBeaver"
                repeat while frontmost
                    keystroke "r" using {command down, shift down}
                    delay 60
                end repeat
            end tell
        end if
    end repeat
end tell

推荐阅读