Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessibility settings for non-GUI apps using AppleScript in OSX Mavericks?

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.

like image 723
varzan Avatar asked Feb 20 '14 16:02

varzan


2 Answers

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)

like image 200
Tommie C. Avatar answered Oct 14 '22 02:10

Tommie C.


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

like image 35
5v16no0 Avatar answered Oct 14 '22 02:10

5v16no0