9
votes

In previous versions of OS X, to allow AppleScript to be run you needed to check “Enable access for assistive devices” in the Accessibility pane of System Preferences.

With Mavericks, this is now a per-app setting that is enabled from Security & Privacy -> Accessibility. How do I enable assistive access if the script is being run from the command line with osascript from within a Jenkins task? As far as I understand, there is no GUI task to authorize then; I get no message dialog asking for permissions. The error message I get in the Jenkins output is:

take_screenshot_iossim.sh:246:303: execution error: System Events got an error: osascript is not allowed assistive access. (-1719)

I also tried adding all applications to the permissions dialog and still receive the permissions denied error.

3
could you provide the permissions denied error message ?Thomas Ayoub
@Samoth edited the post, added errorvarzan
If I understand well you issue, Jenkins execute your osascript and throw the error ?Thomas Ayoub
Yes, that is the problem.varzan

3 Answers

7
votes

Problem

If I understand correctly, you want to enable GUI scripting from a command line.

Solution

I believe you will need to use the following code to enable GUI Scripting from within an applescript. That script can then be executed from the command line. You may want to exclude the display dialog as needed -- this script just presents the dialog to confirm that the GUI settings have been made active.

if enabledGUISCripting(true) then
    -- GUI Scripting statements go here
    display dialog "GUI Scripting is enabled"
else
    --non-GUI scripting statements go here
    display dialog "GUI Scripting is disabled"
end if

on enabledGUISCripting(switch)
    tell application "System Events"
        activate
        if not (UI elements enabled) then set (UI elements enabled) to true
        return (UI elements enabled)
    end tell
end enabledGUISCripting

The key parts of this are:

tell application "System Events"
    activate
    if not (UI elements enabled) then set (UI elements enabled) to true
end tell

You could combine the key parts mentioned above into a file and tell osascript to execute it using:

osascript file.scpt

If you want to send osascript multiple commands on a single line instead of using a script file you could try (note multiple -e flags per command):

osascript -e “line 1″ -e “line 2″ -e “line 3″

You may want to run this using sudo (if needed):

If you get an error such as this one when running such scripts from Terminal you will need to enable accessibility for Terminal in the privacy preferences (Mavericks, see image below)

execution error: System Events got an error: Can’t set UI elements enabled of application to true. (-10006)

Enabling Terminal Privacy

System Preferences > Security & Privacy > Accessibility (Click to enable Terminal)

1
votes

I worked round this by starting the jenkins slave by using JNLP rather than SSH. You can set this up in the slave node settings on the jenkins master.

To set this up i created an automator script which runs at login so the jenkins slave runs as a real user.

curl -o slave.jar http://server:8080/jnlpJars/slave.jar && java -jar slave.jar -jnlpUrl http://server:8080/computer/<slaveName>/slave-agent.jnlp -secret <secret from the node settings on master>

This causes osx to popup an enable application access to assistive devices popup for the automator script which then allows it to access everything.

For me this is not a complete solution as i have problems with JNLP slaves going offline more than the SSH launched slaves, which is how i found this issue :D

0
votes

Another solution is to run the command by spawning a terminal which i guess is not in apple's banned list of things for assitive access.

After a bit of messing around i found the following worked over ssh

osascript -e 'tell application "Terminal" to do script "take_screenshot_iossim.sh"'

This then pops up the assitive dialog again and you can approve access

You need to setup the terminal to auto close after success otherwise you'll end up with millions of them :D