I am trying to access the Privacy - > Accessibility tab using Applescript. Can anyone help me? I need to output a list of all programs in the terminal.
I think, but not correctly
osascript -e 'tell application "System Preferences" set securityPane to pane id "com.apple.preference.security" to get the name of every Privacy_Accessibility'
The example AppleScript code, shown below, was tested in Script Editor under macOS Catalina and macOS Big Sur with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
It was also tested used as a shell script with a #!/usr/bin/osascript
shebang.
Example AppleScript code:
-- # Set the value of targetRow to the name of the target row as shown
-- # in Privacy tab of Security & Privacy pane in System Preferences.
set targetRow to "Accessibility"
-- # Do not modify the code below unless necessary.
property |System Preferences| : name of ¬
application id "com.apple.systempreferences"
-- # Check to see if System Preferences is
-- # running and if yes, then close it.
-- #
-- # This is done so the script will not fail
-- # if it is running and a modal sheet is
-- # showing, hence the use of 'killall'
-- # as 'quit' fails when done so, if it is.
-- #
-- # This is also done to allow default behaviors
-- # to be predictable from a clean occurrence.
if running of application id "com.apple.systempreferences" then
try
tell application id "com.apple.systempreferences" to quit
on error
do shell script "killall '" & |System Preferences| & "'"
end try
delay 0.1
end if
-- # Make sure System Preferences is not running before
-- # opening it again. Otherwise there can be an issue
-- # when trying to reopen it while it's actually closing.
repeat while running of ¬
application id "com.apple.systempreferences" is true
delay 0.1
end repeat
-- # Open System Preferences to the Security & Privacy pane.
tell application id "com.apple.systempreferences"
run -- # Used as a workaround to an issue in Big Sur.
delay 0.1
reveal anchor "Privacy_Accessibility" of ¬
pane id "com.apple.preference.security"
end tell
-- # Use System Events to achieve the goal.
tell application id "com.apple.systemevents"
run -- # Used as a workaround to an issue in Big Sur.
delay 0.2
tell window 1 of application process |System Preferences|
-- # Wait for target pane to be available.
my waitForUIelement(row -1 of table 1 of scroll area 1 of tab group 1)
-- # Ascertain the target row to select.
set rowTargetRow to first item of ¬
(rows of table 1 of scroll area 1 of tab group 1 ¬
whose value of static text 1 of UI element 1 is targetRow)
-- # Select the target row.
select rowTargetRow
-- # Wait for target UI element to be available.
my waitForUIelement(button 1 of group 1 of group 1 of tab group 1)
-- # Get the names of the applications.
set appNames to the value of ¬
static text 1 of UI element 1 of ¬
rows of table 1 of scroll area 1 of group 1 of tab group 1
end tell
end tell
delay 0.02
quit application id "com.apple.systempreferences"
-- # Output the names of the applications.
return appNames
-- ## Handler ##
on waitForUIelement(uiElement)
tell application id "com.apple.systemevents"
tell window 1 of application process ¬
|System Preferences|
set i to 0
repeat until exists uiElement
delay 0.1
set i to i + 1
if i ≥ 40 then return
end repeat
end tell
end tell
end waitForUIelement
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.