Assistive Access when script is launched by agent
A simple fix is to save the script as a text file instead.
At the top of the script add the osascript
shebang
#!/usr/bin/osascript
example:
#!/usr/bin/osascript
say "hello"
tell application "Safari" to activate
tell application "System Events"
delay 2
keystroke "p" using command down
tell application process "Safari"
tell application "System Events"
tell process "Safari"
click menu button "PDF" of sheet 1 of window 1
delay 1
click menu item "Save PDF to Web Receipts Folder" of menu of menu button "PDF" of sheet 1 of window 1
end tell
end tell
end tell
end tell
The in the save dialog choose Text as the file format.
The file will be saved as plain text but with the .applescript
extension.
In terminal make the save script text file executable.
I used:
chmod +x /Users/UserName/Scripts/newTest1.applescript
In the command arguments of the LaunchAgent just add the path to the file.
Do not add the osascript command to the arguments. You do not need it.
The saved script text file will act like an executable shell script.
When you first load or run the LaunchAgents you will get a prompt to set the Assistive Access in System Preferences. If you already have System preferences open you will not but the Script text file will be added to the list.
You now just have to check its check box to allow it.
I would reload the LaunchAgent so it picks up straight away.
I have double checked this with the script above and all works as expected.
While I have accepted @markhunte's question because it works in the general case, it did not work for me. That is why I post my own solution as an alternative.
Create a shellscript that launches the applescript
By having the launchagent run a shellscript that simply calls to the applescript (.scpt) the shellscript needs to be allowed access to assistive devices once and can be run by the launchagent to, in turn, call the applescript.
In my case, I ended up with the following LaunchAgent .plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.mylabel.some-awesome-utility</string>
<key>Program</key>
<string>/Users/username/absolute/path/to/shellscript.sh</string>
<key>StandardErrorPath</key>
<string>/tmp/com.mylabel.some-awesome-utility.err</string>
<key>StandardOutPath</key>
<string>/tmp/com.mylabel.some-awesome-utility.out</string>
<key>StartCalendarInterval</key>
<array>
<dict>
<key>Hour</key>
<integer>06</integer>
<key>Minute</key>
<integer>05</integer>
</dict>
<dict>
<key>Hour</key>
<integer>20</integer>
<key>Minute</key>
<integer>05</integer>
</dict>
</array>
</dict>
</plist>
This LaunchAgent will call the shellscript.sh
twice daily as specified by the StartCalendarInterval
key. Also note the StandardErrorPath
and StandardOutPath
keys that were useful while debugging.
The shellscript.sh is extremely simple:
#!/bin/sh
osascript /Users/mnmt/Documents/InCuffs/Orders/PrepareShipping/print-shipping-labels.scpt
As you can see, a simple shebang line to specify that it is indeed a shellscript followed by a single call to the applescript I had originally been calling directly from the LaunchAgent.