Mac OSX11 (BigSur) - changing Region / Country (locale) via appleScript - 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.

Related

Applescript System Preferences automation

I'm working on automating setting system preferences, but I have a problem. This code should select Wi-Fi tab but scroll area 1 does not exist unless I click any element that belongs to scroll area manually. I tried emulating click with many external programs but even then I can't access scroll area
tell application "System Preferences"
activate
reveal pane id "com.apple.preference.dock"
end tell
tell application "System Events" to tell application process "System Preferences"
delay 1
tell scroll area 1 of window 1
select row 3 of outline 1
end tell
end tell
Is there any other way to change Dock & Menu Bar settings or just to access scroll area items?
Edit: The end goal is to hide Wi-Fi icon from menu bar.
The end goal is to hide Wi-Fi icon from menu bar.
UI Scripting of System Preferences in macOS Big Sur has become a nightmare, as many of the methods that used to work in previous versions of macOS just no longer do in macOS Big Sur. Many UI elements report Parent does not report element as one of its children when using Accessibility Inspector of Xcode, which then make it impossible to communicate with them. Or some code may work one time and then not the next. I wrote some code that opened to Wi-Fi and clicked the Show in Menu Bar checkbox. It worked a few times and now it doesn't.
The original code I wrote which sporadically worked I'll not post, however, the following example AppleScript code does consistently work as tested under macOS Big Sur 11.4, albeit it is what I consider kludgy UI Scripting, as it's visible on screen, is prone to failure due to timing issues, or if the hierarchical UI element structures change due to macOS updates/upgrades.
The example AppleScript code, shown below, was tested in Script Editor under macOS Big Sur 11.4 with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
1 Assumes necessary and appropriate setting in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
This script requires that the Use keyboard navigation to move focus between controls checkbox is checked on the System Preferences > Keyboard > Shortcuts tab, and as coded, the script checks its status and toggles the checkbox, as necessary, based on its current status.
This script also first checks to see if the Wi-Fi icon is shown on the Menu Bar and if not, then halt execution of the script, as its purpose is to act only if it is shown on the Menu Bar.
Example AppleScript code:
-- # 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) & ¬
"com.apple.controlcenter.plist"
-- Get the value of 'NSStatusItem Visible WiFi' to determine if the
-- Wi-Fi icon is showing on the Menu Bar, and if it's not, then halt
-- execution of the script, as its purpose is to act only if it is.
tell application "System Events" to ¬
tell the property list file thePropertyListFilePath to ¬
set |Wi-Fi Menu Bar Icon Status| to the value of ¬
the property list item ¬
"NSStatusItem Visible WiFi"
if |Wi-Fi Menu Bar Icon Status| is false then return
-- # 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 "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
-- # 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 "System Preferences" is true
delay 0.1
end repeat
-- # 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 toggleKeyboardNavagition()
end if
-- # Open System Preferences to the Dock & Menu Bar pane.
-- #
-- # This UI Script needs it to be visible, hence the activate command.
tell application "System Preferences"
activate
reveal pane id "com.apple.preference.dock"
end tell
tell application "System Events"
set i to 0
repeat until exists window "Dock & Menu Bar" of ¬
application process "System Preferences"
delay 0.1
set i to i + 1
if i ≥ 30 then return
end repeat
end tell
-- # Tab to the 'Show in Menu Bar' checkbox and uncheck it.
tell application "System Events"
key code 48 -- # tab key
delay 0.2
key code 125 -- # down arrow key
delay 0.2
key code 48 -- # tab key
delay 0.2
key code 49 -- # spacebar
delay 0.1
end tell
if keyboardNavigation = 0 then
-- # Uncheck the checkbox if it
-- # was previously unchecked.
my toggleKeyboardNavagition()
end if
delay 0.2
tell application "System Preferences" to quit
-- # Handler(s) #
-- # Toggles checkbox: 'Use keyboard navigation
-- # to move focus between controls'
on toggleKeyboardNavagition()
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 toggleKeyboardNavagition
Notes:
If the normal state of the Use keyboard navigation to move focus between controls checkbox is unchecked, then do not run the script immediately back to back as it takes a second or two for the value of the property list item "AppleKeyboardUIMode" in the users global preferences file to update the change. I'm mentioning this mainly for when doing testing more so than when in normal production use, as it shouldn't be an issue then.
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.

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"

Choose Items in Security and Privacy

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.

Applescript delay issue

I am testing applescripts that I will use later in my OSX app.
I'm getting a 6 sec delay after the click button command below.
After some research it seems that this is a known issue.
What I find interesting is, if i use the commercial app QuicKeys to perform the same
button click there is no delay, so I assume they found a work around.
Anybody have any ideas?
tell application "System Events"
tell process "Pro Tools"
set frontmost to 1
click button "Track List pop-up" of window 1
-- 6 seconds delay before next command is sent
key code 36 -- return key stroke
end tell
end tell
Was having the same problem and resolved it by enclosing the click causing delay in the ignoring application responses block. Here is a quick summary:
OLD CODE (Causes 6 sec delay)
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
click bt
tell (first menu item whose title is "SBH80") of menu of bt
click
tell menu 1
if exists menu item "Disconnect" then
click menu item "Disconnect"
else
click menu item "Connect"
end if
end tell
end tell
end tell
NEW CODE (No delay)
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
ignoring application responses
click bt
end ignoring
end tell
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "SystemUIServer"
tell (first menu item whose title is "SBH80") of menu of bt
click
tell menu 1
if exists menu item "Disconnect" then
click menu item "Disconnect"
else
click menu item "Connect"
end if
end tell
end tell
end tell
Please check detailed answer in the thread listed below.
Speed up AppleScript UI scripting?
Hope this helps.
It seems click or axpress causes a big delay.
Instead - get position and use a third party shell script to do the clicking. Much Much faster.
using clicclik : https://www.bluem.net/en/mac/cliclick/
put in user library/application support/Click
set clickCommandPath to ((path to application support from user domain) as string) & "Click:cliclick"
set clickCommandPosix to POSIX path of clickCommandPath
tell application "System Events"
tell process "Pro Tools"
set frontmost to 1
tell button "Track List pop-up" of window 1
set {xPosition, yPosition} to position
set x to xPosition
set y to yPosition
end tell
do shell script quoted form of clickCommandPosix & " c:" & xPosition & "," & yPosition
key code 36 -- return key stroke
end tell
end tell

Resources