Search code examples
xcodeshellapplescriptapplescript-objc

Automatically Add Application to Allow assistive access


Part of an application changes the scroll direction of the trackpad with this AppleScript (used as AppleScriptObjC in Xcode AppleScript application).

When running, it often pops up with a message telling me that my app has not been allowed accessibility access (the usual message...)

Here is the code:

on TrackpadIsAttached()
    try
        tell application "System Preferences" to quit
        tell application "System Preferences"
            activate
            set current pane to pane "com.apple.preference.trackpad"
        end tell
        tell application "System Events" to tell application process "System Preferences"
            tell radio button 2 of tab group 1 of window 1 to if value is 0 then click
            tell checkbox 1 of tab group 1 of window 1 to if value is 1 then click
        end tell
        tell application "System Preferences" to quit
        functionSuccessful("Mouse control optimisation completed successfully.")
     on error errMsg
         my errorReporting(errMsg, "Fatal Mouse Optimisation (Trackpad) Error")
         tell application "System Preferences" to quit
     end try
end TrackpadIsAttached

I know I can do it manually with: System Preferences > Security & Privacy > Privacy > Accessibility but can it also be done automatically with AppleScriptObjC or 'Do Shell Script'? - I don't mind if the user has to type is a password to authenticate it etc.

Any help is greatly appreciated.


Solution

  • Two things:

    1. GUI Scripting is the automation option of absolutely last resort: humiliatingly klunky and brittle as hell, and enabling it either manually or automatically opens up a potential security risk—not a liability you want to place on either yourself or your users if you can possibly help it. If there is any programmatic way to perform the operation you need, use that instead. Which leads us to…

    2. You haven't said why your users need and/or want to change their trackpad direction. What is the purpose of your app? Is it intended to give users who frequently reverse trackpad direction (e.g. while playing third-party games) with an easier way to do it? Or it trying to reverse it for its own purposes? (e.g. If your app just needs to know which way the user's finger is really moving, use -[NSEvent isDirectionInvertedFromDevice].)

    Update your question to explain your overall goal, and you're a lot more likely to receive robust high-quality answers that won't further dig you into any holes of your own unintended making.