Applescript exits with error when run via launchd

Solution 1:

As bjbk commented, the problem was that osascript did not have Assistive Access. While it is easy to add .app programs to this list (System Preferences > Security & Privacy > Accessibility), OS X doesn't allow binaries to be added through the GUI. See Jacob Palmela's blog for how to add a program via the command line, or just copy and paste the following command, substituting in the path of the program to be added where indicated before executing:

sudo sqlite3 /Library/Application\ Support/com.apple.TCC/TCC.db "INSERT or REPLACE INTO access VALUES('kTCCServiceAccessibility','PROGRAM-TO-BE-ADDED',1,1,1,NULL)"

sudo is necessary because the sqlite3 db file is owned by root.