2

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}
user3439894
  • 7,266
  • 3
  • 17
  • 28
gibbz00
  • 1,947
  • 1
  • 19
  • 31
  • 1
    This might be related to permissions. Check: System Preferences > Security & Privacy > Privacy > Accessibility, does Script Editor/Automater have accessibility permissions? – wp78de Feb 17 '20 at 18:35
  • @wp78de yes, there were initially permission problems. I fixed the problem by going to the settings option you are alluding to. I saved this script as an app and it seems to be running fine. – gibbz00 Feb 17 '20 at 18:42
  • What are you doing to repeat the keystroke? – red_menace Feb 17 '20 at 18:52
  • @wp78de its not working despite fixing the permissions. Nothing happens when the app (script) is running. – gibbz00 Feb 17 '20 at 18:56
  • @red_menace nothing so far. I do not know how to repeat the keystrokes every 60 seconds. – gibbz00 Feb 17 '20 at 18:57
  • AFAIK, DBeaver is a Eclipse/Java app and I know keyboard shortcuts do not work that well with those at times https://github.com/dbeaver/dbeaver/issues/3670 – wp78de Feb 17 '20 at 19:23
  • @wp78de 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}` – gibbz00 Feb 17 '20 at 20:25
  • Running the simpler version of the code on a schedule does not work for you, does it? – wp78de Feb 17 '20 at 22:00

2 Answers2

1

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
wch1zpink
  • 3,026
  • 1
  • 8
  • 19
1

You want to avoid using something like a repeat loop, as that will block the app's user interface (for quitting or to just avoid the spinning wheel of death). A relatively easy way to repeat stuff like that is to make a stay-open application, and put your repeating code in the idle handler, which uses a timer - for example:

on idle
    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
    return 60 -- do it again in 60 seconds
end

The statements in the idle handler are run when the app is idle; the return value determines the number of seconds before the handler is run again.

red_menace
  • 3,162
  • 2
  • 10
  • 18