Search code examples
macosapplescript

Detect which anchor is selected in Security & Privacy tab


I am currently trying to detect which "anchor" is selected in the Privacy pane of Security & Privacy. With the following code, I am able to get a list of all the available anchors but I am at a loss how to extract the currently selected one:

tell application "System Preferences"
    activate
    get the name of every anchor of current pane
end tell

result:

{"Privacy_Microphone", "Privacy_Reminders", "Privacy_Calendars", "Firewall", "Privacy_Assistive", "Privacy_LocationServices", "Privacy_Contacts", "General", "Advanced", "Privacy_Accessibility", "Privacy_Camera", "Privacy_SystemServices", "FDE", "Privacy", "Privacy_AllFiles"}

Right now I am trying to detect if the Accessibility (Privacy_Accessibility) anchor is being selected. I have tried looking through ~20 related SO questions, however none of them offer a solution to the currently selected element.

Many thanks

enter image description here


Solution

  • The "System Preferences" application has a weak dictionary for direct scripting. Therefore, you often have to use GUI scripting, which is undesirable.

    Do you really need to know which anchor is selected? It may be better to reveal the anchor you need. In this case, GUI scripting would not be needed.

    Anyway, here is GUI-scripting solution for Catalina OS, which gives name of selected anchor:

    selectedAnchorName()
    
    on selectedAnchorName()
        try
            tell application "System Preferences" to activate
            tell application "System Events" to tell process "System Preferences"
                tell window 1 to tell tab group 1 to tell scroll area 1 to tell table 1
                    return name of UI element 1 of (get 1st row whose selected is true)
                end tell
            end tell
        end try
        return false
    end selectedAnchorName