Search code examples
macosapplescriptmacos-mojaveosascript

macOS send keystroke to the active app periodically


I am trying to send a keystroke (command+ shift+ r) to a macOS (Mojave) app called "Dbeaver" every minute as long as DBeaver is the active app. I have tried the following with no effect.

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

The script works perfectly fine if its simple like the following:

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

Solution

  • I do not have the application you referred to but I tested this following AppleScript code using TextEdit.app and it worked. Let me know if you run into any errors or issues

    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