macosapplescriptapplescript-objcnsapplescriptapplescript-studio

Privacy tab using AppleScript


I am trying to access the Privacy -> Accessibility tab using Applescript. Can anyone help me?

I need to display a list of all the programs in the section:

The request itself and the output in the terminal using osascript -e.

It is necessary to exclude interaction with the GUI. Here's what I managed to find

osascript -e 'tell application "System Events" to get the name of every login item'

I need to find the same solution for Accessibility? And get the result the same as in the screenshot below.

The main goals are

  1. Locally get the information contained in Security & Privacy 2)Connect to mac OS via SSH and get the information contained in Security & Privacy. If this is not possible, then how to display the information using a single Apple script.

Solution

  • It is necessary to exclude interaction with the GUI (on the remote system).

    Testing with the remote system being macOS Big Sur 11.6 and having checked [√] Allow full disk access for remote users in System Preferences > Sharing > Remote Login on the remote system, then the example shell script code executed in Terminal on the local system in a ssh session to the remote system will give you a raw dump of what's listed under Accessibility in System Preferences > Security & Privacy > Privacy without the need for UI Scripting with AppleScript.

    sqlite3 '/Library/Application Support/com.apple.TCC/TCC.db' 'SELECT client FROM access WHERE service="kTCCServiceAccessibility";'
    

    On the test system its output was:

    /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/AE.framework/Versions/A/Support/AEServer
    com.apple.AccessibilityInspector
    com.apple.Automator
    com.apple.ScriptEditor2
    com.apple.Terminal
    com.latenightsw.ScriptDebugger8
    

    If you really have a need to use AppleScript you could, however, say you needed the output to be pretty. In other words, using an AppleScript script saved as a shell script using a #!/usr/bin/osascript shebang the output on the same remote system would be e.g.:

    AEServer, Accessibility Inspector, Automator, Script Editor, Terminal, Script Debugger
    

    Example AppleScript code:

    #!/usr/bin/osascript
    
    set theAccessibilityList to paragraphs of (do shell script "sqlite3 '/Library/Application Support/com.apple.TCC/TCC.db' 'SELECT client FROM access WHERE service=\"kTCCServiceAccessibility\";'")
    
    set theAccessibilityApplicationNamesList to {}
    repeat with thisItem in theAccessibilityList
        if thisItem starts with "/" then
            set shellCommand to (do shell script "f=" & quoted form of thisItem & "; echo ${f##*/}")
            set end of theAccessibilityApplicationNamesList to shellCommand
        else
            try
                set end of theAccessibilityApplicationNamesList to the name of application id thisItem
            end try
        end if
    end repeat
    
    return theAccessibilityApplicationNamesList
    

    Notes:

    I created, saved and made executable the example AppleScript code, shown above, on the local system and then copied it the from the local system to the remote system using scp.



    Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.