r/applescript • u/Son_of_a_Shepherd • Nov 03 '22
macOS Ventura System Settings with System Events automation template
Updated Post
This Post is outdated!
Figured I would share the script I made to go to specific pages in the System Settings app using system events. I haven't made this script dynamic as of yet, so I am not sure it will work universally (especially if you to not have family sharing enable or java). I will post a second code snippet below that I used to generate a list that I used to convert to the dictionary incase the values are off for you.
You can split the System Settings tell at splitter group 1 and use group 2 to interact with the actual setting for the pane.
use framework "Foundation"
property pane_ids : {|AppleID|:2, |Family|:3, |Wi-Fi|:5, |Bluetooth|:6, |Network|:7, |Notifications|:9, |Sound|:10, |Focus|:11, |Screen Time|:12, |General|:14, |Appearance|:15, |Accessibility|:16, |Control Center|:17, |Siri & Spotlight|:18, |Privacy & Security|:19, |Desktop & Dock|:21, |Displays|:22, |Wallpaper|:23, |Screen Saver|:24, |Battery|:25, |Lock Screen|:27, |Touch ID & Password|:28, |Users & Groups|:29, |Passwords|:31, |Internet Accounts|:32, |Game Center|:33, |Wallet & ApplePay|:34, |Keyboard|:36, |Trackpad|:37, |Printers & Scanners|:38, |Java|:40}
on open_settings_to(settings_pane)
set pane to current application's NSDictionary's dictionaryWithDictionary:pane_ids
set pane_index to (pane's valueForKey:settings_pane) as anything
tell application "System Settings"
activate
delay 1
end tell
tell application "System Events"
tell application process "System Settings"
tell outline 1 of scroll area 1 of group 1 of splitter group 1 of group 1 of window 1
select row pane_index
end tell
end tell
end tell
end open_settings_to
Here is the code used to get the list of settings names and indices:
on get_settings_list()
tell application "System Settings"
activate
delay 1
end tell
tell application "System Events"
tell application process "System Settings"
set row_list to {}
set row_num to 0
tell outline 1 of scroll area 1 of group 1 of splitter group 1 of group 1 of window 1
repeat with r in rows
set row_num to row_num + 1
if UI element 1 of r exists then
tell UI element 1 of r
repeat with x in UI elements
if class of x is static text then
set row_name to name of x as string
set val to {row_name, row_num}
copy val to end of row_list
end if
end repeat
end tell
end if
end repeat
end tell
end tell
end tell
return row_list
end get_settings_listHope this helps with updating your scripts.
Edit: You can speed up the script and make it more reliable by placing the following before tells to any UI elements:
repeat until *Path to UI element* exists
delay 0
end repeatExample:
repeat until splitter group 1 of group 1 of window 1 exists
delay 0
end repeat
tell splitter group 1 of group 1 of window 1
...
Edit 2: Found another issue. When a software update is available the, the pane indexes are shifted. Will post a fix later today. Fixed:
on open_settings_to(settings_pane)
set pane to current application's NSDictionary's dictionaryWithDictionary:pane_ids
set pane_index to (pane's valueForKey:settings_pane) as anything
tell application "System Settings"
activate
delay 1
end tell
tell application "System Events"
tell application process "System Settings"
tell splitter group 1 of group 1 of window 1
tell outline 1 of scroll area 1 of group 1
if name of static text of UI element 5 = "Wi‑Fi" then
select row pane_index
else
set pane_index to pane_index + 2
select row pane_index
end if
end tell
end tell
end tell
end tell
end open_settings_to
3
u/Lowfihifi Nov 07 '22 edited Nov 07 '22
Thank you - this is fantastic u/Son_of_a_Shepherd
---
I had a script that was working well before (selecting an audio output) which obviously broke in Ventura. Pasting below in case it's of any use to anyone else.
The script selects the first Output in the Sound pane (row 1), then reactivates the Music app and quits System Settings. You could change it to select something different (or specify an actual output name? Not sure...way beyond my skills).
I was struggling for 30 minutes, until I realised that the
delay 1was vitally important 🤦🏻♂️.2
u/sekhmet666 Nov 03 '22 edited Nov 03 '22
Awesome!
How do you get it to click, say, the "Hot Corners..." button in the "Desktop & Dock" page? I've tried many path combinations (from your example) but can't figure out the correct one. For example I tried:
tell application process "System Settings"tell outline 1 of scroll area 1 of group 1 of splitter group 1 of group 1 of window 1click button "Hot Corners…"end tellend tellThe hierarchy to that button according to the Accessibility Inspector app is this, but can't seem to get it to work:
window > group > split group > group > group > scroll area > buton
2
u/CounterBJJ Nov 03 '22
Sorry if my question is stupid and/or the answer obvious (I'm not a coder), but can your script or part of it be used to change Keyboard settings (and more specifically the spelling language)?
I had an awesome script made by a Redditor on this sub that allowed me to toggle between US English and French (I need to do this often for work), but Ventura broke it.
The old script is mentioned in this thread:
https://www.reddit.com/r/applescript/comments/ycnt3r/applescript_to_change_spelling_language_no_longer/
Thanks.
1
u/Leopold-TAO Nov 23 '22 edited Nov 23 '22
Thanks a lot!
I have a problem with my script. Can you help me to fix it?
My monitor can not work with the feature "variable refresh rate", I must use a fixed refresh rate to make my monitor work.
Every time I rewake or restart my Macbook, the refresh rate always returns to "variable", so I wrote a script with a keyboard shortcut to light up the monitor when it has no signal.
But I have a problem with choosing a specific monitor. Here is my code.
According to Accessibility Inspector in Xcode, choosing monitor action should use the command
click button 1 of scroll area 1. I have tried many times, but it does not work.This script only works when the Macbook lip is closed. If there are two or more monitors activated, it will not enter the specific monitor setting correctly.
1
u/copperdomebodha Dec 02 '22 edited Dec 02 '22
So, I have the following code for opening the named preference pane. I am looking at the Displays pane and trying to determine how to select a monitor from the two displayed in "scroll area 1" of the pane. It shows only two buttons ( "Arrange" and the pop-up button for remote screens ). I cannot find any reference to the two displays shown.
Does anyone know what reference will let me select the second monitor?
And for a quicker Panel open...
2
u/TheJmaster Nov 03 '22
Very cool – thanks for sharing.
1
u/fuckinatodaso Jan 10 '23
I know this is an old thread but hoping for some help here as I am struggling! I am trying to change my monitor's resolution to the second scaled option in the list but am unsuccessful. Here's what I've got, based off of what was submitted previously by /u/Leopold-TAO :
1
u/cheesecakegood Jan 11 '23 edited Jan 11 '23
Related, but different: I spent forever trying to script a toggle for the tap-to-click trackpad functionality. I finally got it to work!
I'm writing this in case anyone wants to try the same route. I ended up using Accessibility Inspector target mode to identify the element I wanted. I looked at the Hierarchy to find the right sequence of groups, splitters, scroll groups, etc. Then I built a long set of tells with UI Element command in the middle to brute force my way into figuring out which was the right path to follow in the tree (it displays the possible UI elements in the console output, comma separated). I ended up finding the right set of options buried several layers down, that I could then condense into one command.
This is what my workflow looked like, since it's hard to describe: https://imgur.com/a/IHqysZc
My code is here, but please note that I'm a noob for scripting and also your results might vary. For example, if you disable Force Click, it actually creates another "checkbox" that then becomes "checkbox 2" and makes the "tap to click" "checkbox" become "checkbox 3". I also just edited it so it might be a hair slower but handles unexpected things better, more reliable overall.
1
u/No_Personality_4696 Feb 14 '23
Many thanks for sharing this. With your updated code for when a System Update is present, the AppleScript code keeps telling me it cannot make "name of static text of UI element 5" into a string, so it cannot compare it to "Wi-Fi" string properly. I don't know if adding "as string" somewhere would help, but regardless of whether or not a system update is present, it always goes to the "else" portion of the branch.
Is there a way to debug AppleScript evaluations like as a "Watch Variables" window in a normal debugger?
1
u/areyouhourly- Feb 15 '23
Would it be possible to use this to enable hdr on all external monitors if it was off? I can’t seem to figure it out with Ventura.
1
u/[deleted] Feb 15 '23
[removed] — view removed comment