AppleScript runs in Automator, but fails when launched via a shortcut

I have the following AppleScript in a service using Run AppleScript in Automator:

on run {input, parameters}
    set date_ to short date string of (current date)
    tell application "System Events"
        keystroke date_
    end tell
end run

It is set to receive no input in any application, and the output replaces text. If I put the cursor in the editing window in Automator and click the run button, it works, and I get the output, e.g.:

...
end run8/15/18

I bound this to a keyboard shortcut (System Properties -> Keyboard -> Shortcuts -> select my script and bind a key combination). However, when I press the key combination, regardless of the application, I briefly (1-2 seconds) get a little spinning gear in the menu bar, and the following error in the console:

Detected missing constraints for < private>. It cannot be placed because there are not enough constraints to fully define the size and origin. Add the missing constraints, or set translatesAutoresizingMaskIntoConstraints=YES and constraints will be generated for you. If this view is laid out manually on macOS 10.12 and later, you may choose to not call [super layout] from your override. Set a breakpoint on DETECTED_MISSING_CONSTRAINTS to debug. This error will only be logged once.

The date is not added to any application where I do this. I've seen one other person with a similar issue here (https://www.alfredforum.com/topic/10311-applescripts-no-longer-work-script-editor-hangs-not-responding-on-sierra/) but they didn't get an answer either. This is confusing, as it appears to be related to window rendering, but I have no GUI elements in my script, AFAIK.

I'm running:

Mac OS Sierra 10.12.6 (16G1510)
Automator Version 2.7 (428)

Ok, while researching for this post I found this post(https://superuser.com/questions/153890/assign-a-shortcut-to-running-a-script-in-os-x/264943) that claims that there is a bug that prevents system services from running after Snow Leopard. Anyone know a workaround without third party tools?


Solution 1:

I was able to get it to work by saving the AppleScript as a separate application, then creating an Automator Service that simply launches that app. Here are the steps:

  • Create a new AppleScript in Script Editor and paste in the following code (yours, but with a slight addition):

    on run
        set date_ to short date string of (current date)
        tell application "System Events"
            set visible of (first process where it is frontmost) to false -- return focus to initiating app
            delay 0.1 -- make sure the application has had time to regain focus—if the script doesn't work, try upping this value a little
            keystroke date_
        end tell
    end run
    
  • Save the script as an Application.

  • Create a new Service in Automator and add a Launch Application action. Set the application to the one you just saved from Script Editor.
  • Link a keyboard shortcut to that Automator action.

I can't verify that it works on Sierra, but it's working on High Sierra 10.13.6.