Search code examples
macosautomationcronapplescript

Osascript via Cron cannot send keystrokes (MacOS)


I have a AppleScript script that I am running through Cron. Within it, contains a line:

tell application "System Events" to keystroke "r" using {command down}

This works fine when I run it locally, via osascript script.scpt.

However, when I run it through Cron, the mailed result says:

execution error: System Events got an error: osascript is not allowed to send keystrokes. (1002)

Here is my crontab for running the script, written through crontab -e:

25 * * * * osascript /Users/eric/scripts/script.scpt

How can I make allow the keystrokes to go through?


Solution

  • Got it working. I had to explicitly give it permission via Security and Privacy:

    System Preferences -> Security and Privacy -> Accessibility -> Plus button

    Then select the Cron executable, which is stored at /usr/sbin/cron. That's all it took!