Enable access for assistive devices programmatically on 10.9

前端 未结 9 2138
庸人自扰
庸人自扰 2020-11-27 11:27

I want to enable access for assistive devices programatically on 10.9. On 10.8 and lower I was using following Applescript to enable access for assistive devices:

         


        
9条回答
  •  醉酒成梦
    2020-11-27 11:44

    Thanks for this shell script samples from @NightFlight, which are really helpful. I used this with AppleScript in a Python application, like the following:

    set sh to "touch /private/var/db/.AccessibilityAPIEnabled && sqlite3 \\"/Library/Application Support/com.apple.TCC/TCC.db\\" \\"INSERT or REPLACE INTO access VALUES('kTCCServiceAccessibility','com.godevnode',0,1,0,NULL);\\""
    do shell script sh with administrator privileges
    

    It worked well for me in Python code as a string.

    Edit (Nov 7, 2014):

    If you want to try this in AppleScript Editor, use a slightly different character escape as below:

    set sh to "touch /private/var/db/.AccessibilityAPIEnabled && sqlite3 \"/Library/Application Support/com.apple.TCC/TCC.db\" \"INSERT or REPLACE INTO access VALUES('kTCCServiceAccessibility','com.godevnode',0,1,0,NULL);\""
    do shell script sh with administrator privileges
    

    For Mac OS X before 10.9, it's even simpler:

    accessibility_api_file = "/private/var/db/.AccessibilityAPIEnabled"
    
    def __enable_accessibility_api():
        try:
            script = 'do shell script "touch %s" with administrator ' \
                     'privileges' % accessibility_api_file
            result = applescript.AppleScript(script).run()
            log.debug("Tried to enable accessibility api, result=" + result)
            return True
        except applescript.ScriptError as err:
            log.error(str(err))
        return False
    

    Just need to touch one file. The AppleScript mentioned in the Python code above can also be used in other languages.

提交回复
热议问题