Automator service works everywhere except when using keyboard shortcut

applescriptautomatorgoogle-chrome

My automator service is as follows:

Workflow receives no input in Google Chrome.app
Input is entire selection (checkbox not checked)

Run Applescript

    on run {input, parameters}
     tell application "Google Chrome" to activate
     tell application "System Events"
      keystroke "i" using {control down, option down}
      keystroke "r"
     end tell
     return input
    end run

In Security & Privacy -> Privacy -> Accessibility, both Automator and Google Chrome are listed and checked, and under Automation, Google Chrome has System Events.app checked.

With Google Chrome open (specifically on a Google Sheets tab), this works:

  • When I run the step from Automator
  • When I run the entire service from Automator
  • When I run the service from the Chrome menu

However, it does not work when I use the keyboard shortcut I assigned in System Preferences: command+option+control+n. A cogwheel briefly appears in the menu bar then goes away, and I even added a notification center action to tell me the service ran (it does) – but nothing happens in Google Sheets. I have tried other keyboard shortcuts to no avail.

Any help appreciated. Honestly if Google would just add a two-stroke "insert row" hotkey, that would relieve me of this whole idiotic exercise 🙂

Best Answer

It is working fine here.

However in my experience most problems using UI sequences are related to the lack of delay for the system to execute the thread. Often it will process the strokes so fast that the actions are not yet finished before automator sends another command, causing overlays and hence ignoring the last action. A simple solution is providing tiny delays between each stroke.

Also the return input line can be causing issues, because the input is empty and there is no parameters.

I have tested this script successfully:

on run
    tell application "Google Chrome" to activate
    delay 0.1
    tell application "System Events"
        key code 34 using {control down, option down}
        delay 0.1
        key code 15
    end tell
end run