macOS定期将击键发送到活动应用程序

问题描述 投票:2回答:2

我试图每分钟向名为“ Dbeaver”的macOS(Mojave)应用发送击键(命令+ shift + r)只要 DBeaver是活动应用。我已经尝试了以下方法,但没有任何效果。

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}
macos applescript macos-mojave osascript
2个回答
0
投票

[您想避免使用类似repeat的循环,因为那样会阻塞应用程序的用户界面(退出或只是避免死机)。重复这样的事情的一种相对简单的方法是制作一个保持打开状态的应用程序,并将重复的代码放入idle处理程序中,该处理程序使用计时器:

on idle
    activate application "DBeaver" 
    tell application "System Events" to keystroke "r" using {command down, shift down}
    return 60 -- do it again in 60 seconds
end

`


0
投票

我没有您所引用的应用程序,但是我使用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
© www.soinside.com 2019 - 2024. All rights reserved.