Choose Items in Security and Privacy - applescript

I am new to AppleScript and I need to know how to choose options in an Apple Window such as Security and Privacy. Also, I would like to choose the options in a specific order as per the picture attached.
After step 3, the applet needs to put in specific credentials which are hardcoded and continue after that.
I only managed to open the application so far with the below code.
tell application "System Preferences"
activate
end tell
delay 1
tell application "System Events"
tell process "System Preferences"
click menu item "Security & Privacy" of menu "View" of menu bar 1
delay 2
tell window "Security & Privacy"
end tell
end tell
end tell
delay 2

I'm offering this as a proof of concept and do not recommend UI scripting of System Preferences > Security & Privacy > Privacy while hardcoding credentials.
The following example AppleScript code was tested under macOS Catalina, and worked for me as coded, however, the value of the delay commands may need to be adjusted to work properly on your system.
This example AppleScript code is written to target the Script Editor checkbox in Full Disk Access of: System Preferences > Security & Privacy > Privacy
Change the value of myUserName and myPassword from missing value to the actual user name and password.
set myUserName to "missing value"
set myPassword to "missing value"
set nameOfRowToSelect to "Full Disk Access"
set appCheckboxToClick to "Script Editor"
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
end if
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
tell application "System Preferences"
activate
reveal anchor "Privacy" of pane "com.apple.preference.security"
end tell
tell application "System Events" to tell application process "System Preferences"
repeat while not (exists window "Security & Privacy")
delay 0.1
end repeat
tell window "Security & Privacy"
keystroke "f" using command down
keystroke tab
delay 0.25
select (first row ¬
of table 1 ¬
of scroll area 1 ¬
of tab group 1 ¬
whose value ¬
of static text ¬
of UI element 1 ¬
contains nameOfRowToSelect)
delay 0.25
click button "Click the lock to make changes."
repeat until exists sheet 1
delay 0.1
end repeat
delay 0.25
tell sheet 1
set value of text field 2 to myUserName
set value of text field 1 to myPassword
delay 0.25
click button "Unlock"
delay 2
end tell
click checkbox 1 ¬
of UI element 1 ¬
of (first row ¬
of table 1 ¬
of scroll area 1 ¬
of group 1 ¬
of tab group 1 ¬
whose (value ¬
of static text ¬
of item 1 ¬
of UI element 1) ¬
contains appCheckboxToClick)
repeat until exists sheet 1
delay 0.1
end repeat
delay 0.25
click button "Later" of sheet 1
delay 0.25
click button "Click the lock to prevent further changes."
delay 0.5
end tell
end tell
quit application "System Preferences"
Note: The example AppleScript code is just that and 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.

Related

Need accessibility help on mac os sound output using applescript

set outputB to 2 --change this to the actual 'line number' of your second desired output
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.sound"
end tell
tell application "System Events"
tell application process "System Preferences"
repeat until exists tab group 1 of window "Sound"
end repeat
tell tab group 1 of window "Sound"
click radio button "Output"
if (selected of row outputA of table 1 of scroll area 1) then
set selected of row outputB of table 1 of scroll area 1 to true
else
set selected of row outputA of table 1 of scroll area 1 to true
end if
end tell
end tell
end tell
--tell application "System Preferences" to quit
I have this apple script from Arthur Hammer. It works perfect if I'm in script editor. When I export it as an application and try to open it from my desktop it only gives me the error "System Events got an error: Output 1 is not allowed assistive access. (-25211)".
I verified that sys pref > security & privacy > privacy > accessibility > Output 1 and script editor are enabled. I've restarted my computer as well but I still can only get that error.
Any ideas how to fix it?

How to click the checkbox of a System Preferences UI element by name

I've got a piece of Applescript that is currently functional and clicks a checkbox in System Prefs > Security & Privacy > Contacts. However, right now it only works because I am explicitly stating the row of the app that I'm targeting (in this case, row 2). This works fine for now, but if in the future I end up with a different app order in that pane, it will break. Is there a way to loop through all the items of a given window and say "if UI element is Alfred 4.app, then click the checkbox"? I'd like to harden the code so that it will work regardless of which order the apps are listed in this pane.
set appName to "Alfred 4.app"
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.security.Privacy_Contacts"
delay 1
end tell
tell application "System Events"
click checkbox 1 of UI element appName of row 2 of table 1 of scroll area 1 of group 1 of tab group 1 of window "Security & Privacy" of application process "System Preferences"
end tell
Following script is tested on the Catalina:
set appName to "Alfred 4.app"
tell application "System Preferences"
activate
reveal anchor "Privacy_Contacts" of pane id "com.apple.preference.security"
end tell
delay 1
tell application "System Events" to tell process "System Preferences"
repeat with aRow in (rows of table 1 of scroll area 1 of group 1 of tab group 1 of window "Security & Privacy")
if name of UI element 1 of aRow is appName then
click checkbox 1 of UI element 1 of aRow
exit repeat
end if
end repeat
end tell

Mac OSX11 (BigSur) - changing Region / Country (locale) via appleScript

I need to automate the change of locale (Region/Country) for format purpose from:
Region: Americas
Country: United States
to
Region: Europe
Country: Spain
So I have the following code, which seems to work up to the point where the system asks to restart the machine (or not).
property theSettings : {"", ""}
set settings1 to {"Americas", "United States"}
set settings2 to {"Europe", "Spain"}
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
delay 0.1
end if
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
tell application "System Preferences"
reveal anchor "Language" of ¬
pane id "com.apple.Localization"
activate
end tell
tell application "System Events"
tell window "Language & Region" of process "System Preferences"
if (value of pop up button "Region:" of tab group 1) is "United States" then
set theSettings to settings2
else
set theSettings to settings1
end if
set {theRegion, theCountry} to theSettings
tell pop up button "Region:" of tab group 1
delay 0.25
click
delay 0.25
click menu item theRegion of menu 1
delay 0.25
click menu item theCountry of menu 1 of menu item theRegion of menu 1
end tell
end tell
delay 1
tell application "System Preferences" to quit
tell sheet 1 of window "Language & Region" of process "System Preferences"
delay 1
keystroke tab
delay 1
keystroke return
end tell
end tell
In the script above, after switching locale, I am trying to click the button Don't Restart. But this does not work as the sheet window does not seem to be active.
Any idea on how to save the settings?
This issue is, at that point the sheet is modal and typically requires physical intervention. In other words, based on its current appearance, normally one would either press enter/return key to accept the default or use the mouse to click either Don’t Restart or Cancel as tabbing in not active by default in this use case.
However, if the [√] Use keyboard navigation to move focus between controls checkbox in System Preferences > Keyboard > Shortcuts is checked, which it is not by default, then one could simply press the space bar as Don’t Restart would have focus even though Restart Now would be, by default, solid blue.
To get past the modality of the situation, one would have to add code that would programmatically check to see that the aforementioned checkbox is checked, and if not check it.
In addition to the check and taking action if necessary, the modifications to your existing code are as follows:
In between:
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
And:
tell application "System Preferences"
reveal anchor "Language" of ¬
pane id "com.apple.Localization"
activate
end tell
Add:
-- # Get the fully qualified POSIX pathname of the target .plist file.
set thePropertyListFilePath to ¬
the POSIX path of ¬
(path to preferences from user domain as string) & ¬
".GlobalPreferences.plist"
-- # Get the value of AppleKeyboardUIMode to determine if the
-- # 'Use keyboard navigation to move focus between controls'
-- # checkbox is checked on the System Preferences >
-- # Keyboard > Shortcuts tab.
tell application "System Events" to ¬
tell the property list file thePropertyListFilePath to ¬
set keyboardNavigation to the value of ¬
the property list item "AppleKeyboardUIMode"
if keyboardNavigation = 0 then
-- # Check the checkbox.
my toggleKeyboardNavigation()
end if
And to the end of the script add:
-- # Handler #
-- # Toggles checkbox: 'Use keyboard navigation
-- # to move focus between controls'
on toggleKeyboardNavigation()
tell application "System Preferences"
activate
reveal anchor "shortcutsTab" of ¬
pane id "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell front window of ¬
application process "System Preferences"
set i to 0
repeat until (exists checkbox 1 of tab group 1)
delay 0.1
set i to i + 1
if i ≥ 30 then return
end repeat
click checkbox 1 of tab group 1
end tell
end tell
end toggleKeyboardNavigation
After making the aforementioned changes, then starting with:
tell application "System Preferences" to quit
Replace it and the ensuing tell block with:
ignoring application responses
tell application "System Preferences" to quit
end ignoring
delay 1
key code 49 -- # space key
Then after the end tell of the primary tell application "System Events" block, the one just below the previous code change, add the following:
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
if keyboardNavigation = 0 then
-- # Uncheck the checkbox if it
-- # was previously unchecked.
my toggleKeyboardNavigation()
delay 0.2
tell application "System Preferences" to quit
end if
Notes:
The example AppleScript code shown herein to check to see if [] Use keyboard navigation to move focus between controls in System Preferences > Keyboard > Shortcuts is checked, can be done differently by opening to that pane and ascertaining whether of not it's checked and act accordingly. The reason I've added the code for it as is, is because I've used it to reset that checkbox later on in the script in the past. If the checkbox is already checked then one doesn't have to see the extra changing of panes.

How to click a button on System Preferences using applescript?

I am trying to write an applescript to change hot corners settings in System Preferences.
Here is what I have got until now.
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.desktopscreeneffect"
end tell
tell application "System Events"
tell process "System Preferences"
click button "Hot Corners..."
end tell
end tell
But I get this error:error "System Events got an error: Can’t get button \"Hot Corners...\" of process \"System Preferences\"." number -1728 from button "Hot Corners..." of process "System Preferences". I would appreciate anyone explaining what is going wrong in here, plus is there any way to get properties(such as available buttons) of a pane?
Change:
click button "Hot Corners..."
To:
click button "Hot Corners…" of tab group 1 of window 1
Note the use of …, an ellipsis vs. ... three dots, as well as the missing UI Scripting hierarchal elements.
After the click line of code, and just to get the various UI elements you'll need to interact with, use the following example AppleScript code to get that info:
get UI elements of sheet 1 of window 1
get UI elements of group "Active Screen Corners" of sheet 1 of window 1
These lines of code do not remain in the finished script.
That all said, this was mainly to point out what was wrong with the code in your question, however the approach in the other answer is the way to go to get to the Hot Corners.
Update to address comment…
what does it mean 'UI Scripting hierarchical elements"
From the AppleScript dictionary for System Events:
UI element n : A piece of the user interface of a process
What this means is there is a hierarchy to the elements that comprise the User Interface, e.g., as shown in the line of code:
button "Hot Corners…" of tab group 1 of window 1
As illustrated in the top section of the following screen shot of Accessibility Inspector, a part of Xcode:
Here is one showing one of the hot corners I have set to show the Desktop:
If you mentioned in your question which drop-down menu in the hot corners that you want to select… I could have posted that additional code in my solution.
This AppleScript code works for me using the latest version of macOS Big Sur.
(* Quits "System Preferences" If Running *)
if application "System Preferences" is running then ¬
do shell script "killall 'System Preferences'"
(* Makes Sure "System Preferences" Is Not Running
Checking Every 1/10 Of A Second Before Moving On
To The Next Command*)
repeat until application "System Preferences" is not running
delay 0.1
end repeat
tell application "System Preferences"
(* Launches "System Preferences" Without Bringing It
To The Foreground, Going Directly To The Window Where You Can
`click button "Hot Corners..."` *)
reveal anchor "ScreenSaverPref_HotCorners" of ¬
pane id "com.apple.preference.desktopscreeneffect"
(* Makes Sure anchor "ScreenSaverPref_HotCorners" exists
Checking Every 1/10 Of A Second Before Moving On
To The Next Command*)
repeat while not (exists of anchor "ScreenSaverPref_HotCorners" of ¬
pane id "com.apple.preference.desktopscreeneffect")
delay 0.1
end repeat
(*.Makes application "System Preferences"
The Frontmost Visible App, Allowing You To Perform
Any Click Commands *)
activate
end tell
delay 0.1
tell application "System Events"
(* "Upper Left Dropdown Menu (pop up button 1) This Can Also Be Repeated
For `(pop up button 2) <-- Bottom Left, (pop up button 3) <-- Upper Right,
& (pop up button 4) <-- Bottom Right" *)
(*. By Now You Should Start Understanding The Purpose And Function
Of These Repeat Loops *)
repeat while not (exists of pop up button 1 of group 1 of sheet 1 of window ¬
"Desktop & Screen Saver" of application process "System Preferences")
delay 0.1
end repeat
click pop up button 1 of group 1 of sheet 1 of window ¬
"Desktop & Screen Saver" of application process "System Preferences"
delay 0.1
repeat while not (exists of menu item "Launchpad" of menu 1 of ¬
pop up button 1 of group 1 of sheet 1 of window "Desktop & Screen Saver" of ¬
application process "System Preferences")
delay 0.1
end repeat
-- Replace "Launchpad" Which Which Ever You Want
click menu item "Launchpad" of menu 1 of pop up button 1 of ¬
group 1 of sheet 1 of window "Desktop & Screen Saver" of ¬
application process "System Preferences"
delay 0.3
click UI element "OK" of sheet 1 of window ¬
"Desktop & Screen Saver" of application process "System Preferences"
end tell
delay 0.5
tell application "System Preferences" to quit

Using AppleScript to uncheck HDR in system preferences

I have a 4K HDR Monitor, and sometimes Macbook causes the colors to be washed, the fix is to Disable and then Re-Enable HDR.
I am trying to create an AppleScript to then incorporate that in Automator to do so.
I was able to get some traction but not able to identify how to do the actual uncheck and identify the group.
Here is what i had so far:
tell application "System Preferences"
activate
delay 2
set the current pane to pane id "com.apple.preference.displays"
delay 2
tell application "System Events"
click checkbox "High Dynamic Range" of group 2 of window "LG HDR 4K" of application process "System Preferences"
end tell
quit end tell
Here is the error I get:
error "System Events got an error: Can’t get group 2 of window \"LG HDR 4K\" of application process \"System Preferences\". Invalid index." number -1719 from group 2 of the window "LG HDR 4K" of application process "System Preferences"
here is a screenshot of the page I am trying to uncheck and re-check HDR:
Any advice would be appreciated, thank you.
I don't have a high-def display, so I don't see this particular option, but if I run the following code, I get a full list of all the subelements of the window:
tell application "System Preferences"
activate
delay 2
set the current pane to pane id "com.apple.preference.displays"
delay 2
tell application "System Events"
tell window 1 of application process "System Preferences"
entire contents
end tell
end tell
end tell
Subelements you are interested in seem to have the following form:
radio button "Scaled" of tab group 1 of window "Built-in Retina Display" of application process "System Preferences" of application "System Events"
Note that it includes a tab group 1 entry (referring to the fact that you are on the 'Display' tab of the four tabs available which is missing from your chain.
The Display pane UI layout has changed a bit in macOS Monterey, and now looks like this:
Display Prefs UI Layout
In my case I wanted to enable HDR on my second monitor, so using the following code I was able to get Script Editor to test whether the High Dynamic Range checkbox was true or false, and if false to enable it:
tell application "System Preferences"
activate
delay 1
set the current pane to pane id "com.apple.preference.displays"
delay 1
tell application "System Events"
click button "Display Settings…" of window "Displays" of application process "System Preferences" of application "System Events"
delay 1
tell sheet 1 of window 1 of application process "System Preferences"
select row 2 of outline 1 of scroll area 1
set theCheckbox to checkbox "High Dynamic Range, Automatically adjust the display to show high dynamic range content."
tell theCheckbox
set checkboxStatus to value of theCheckbox as boolean
if checkboxStatus is false then click theCheckbox
end tell
end tell
end tell
end tell
If I wanted to select a different monitor, I would change the index of "row 2" in this line:
select row 2 of outline 1 of scroll area 1 of sheet 1 of window "Displays" of application process "System Preferences" of application "System Events"

Resources