@@ -37,68 +37,11 @@ set command to "
3737my withTimeout(command, timeoutSeconds)
3838` ;
3939
40- const enableFocusModeVenturaAppleScript = (
41- locale : string ,
42- platformMajor : number
43- ) => {
40+ const enableFocusModeVenturaAppleScript = ( locale : string ) => {
4441 // TODO: attempt to rewrite scripts without any locale specific instructions
4542 // so this setup can be used regardless of user locale preferences.
4643 const center = locale . trim ( ) === "en_GB" ? "Centre" : "Center" ;
4744
48- if ( platformMajor >= 25 ) {
49- return `-- Startup delay to reduce chance of "Application isn't running (-600)" errors
50- delay 1
51-
52- set timeoutSeconds to 30.0
53-
54- tell application "System Events"
55- -- open Control Center using the same keystroke used in your script
56- key down 63
57- keystroke "c"
58- delay 0.3
59- key up "c"
60- key up 63
61- delay 0.5
62- try
63- tell application process "Control Center"
64- set root to window 1
65- my dumpElements(root, 0)
66- end tell
67- on error errMsg
68- log "ERROR: " & errMsg
69- end try
70- end tell
71-
72- on dumpElements(el, indent)
73- try
74- set r to (role of el) as string
75- on error
76- set r to ""
77- end try
78- try
79- set n to (name of el) as string
80- on error
81- set n to ""
82- end try
83- try
84- set idx to (index of el) as string
85- on error
86- set idx to ""
87- end try
88- set prefix to ""
89- repeat indent times
90- set prefix to prefix & " "
91- end repeat
92- log prefix & r & " | " & n & " | index:" & idx
93- try
94- set children to UI elements of el
95- repeat with c in children
96- my dumpElements(c, indent + 1)
97- end repeat
98- end try
99- end dumpElements` ;
100- }
101-
10245 return `-- Startup delay to reduce chance of "Application isn't running (-600)" errors
10346delay 1
10447
@@ -144,6 +87,77 @@ my withTimeout(command, timeoutSeconds)
14487` ;
14588} ;
14689
90+ const enableFocusModeTahoeAppleScript = ( locale : string ) => {
91+ // TODO: attempt to rewrite scripts without any locale specific instructions
92+ // so this setup can be used regardless of user locale preferences.
93+ const center = locale . trim ( ) === "en_GB" ? "Centre" : "Center" ;
94+
95+ return `-- Startup delay to reduce chance of "Application isn't running (-600)" errors
96+ delay 1
97+
98+ on findDoNotDisturb(e)
99+ tell application "System Events"
100+ try
101+ log ("Checking element: " & (name of e) & " (" & (role of e) & ")")
102+
103+ if name of e is "Focus" then
104+ return e
105+ end if
106+ end try
107+
108+ try
109+ repeat with k in UI elements of e
110+ set found to my findDoNotDisturb(k)
111+ if found is not missing value then return found
112+ end repeat
113+ end try
114+ end tell
115+
116+ return missing value
117+ end findDoNotDisturb
118+
119+ -- Open Control Center drop down
120+ tell application "System Events"
121+ key down 63
122+ keystroke "c"
123+ delay 0.5
124+ key up "c"
125+ key up 63
126+ end tell
127+
128+ tell application "System Events"
129+ tell process "Control ${ center } "
130+ set roots to UI elements of window 1
131+
132+ repeat with r in roots
133+ set doNotDisturbCheckbox to my findDoNotDisturb(r)
134+ if doNotDisturbCheckbox is not missing value then exit repeat
135+ end repeat
136+
137+ -- Toggle Do Not Disturb
138+ if doNotDisturbCheckbox is not missing value then
139+ set doNotDisturbCheckboxStatus to value of doNotDisturbCheckbox as boolean
140+
141+ tell doNotDisturbCheckbox
142+ if doNotDisturbCheckboxStatus is false then
143+ perform action 1 of doNotDisturbCheckbox
144+ end if
145+ end tell
146+ end if
147+ end tell
148+ end tell
149+
150+ -- Close Control Center drop down
151+ tell application "System Events"
152+ key down 63
153+ key down "c"
154+ delay 0.5
155+ key up "c"
156+ key up 63
157+ end tell
158+ ` ;
159+ } ;
160+
147161export async function enableDoNotDisturb ( ) {
148162 const platformMajor = Number ( os . version ( ) . split ( "Version " ) [ 1 ] . split ( "." ) [ 0 ] ) ;
149163
@@ -153,12 +167,21 @@ export async function enableDoNotDisturb() {
153167 } else if ( platformMajor === 21 ) {
154168 // From MacOS 12 Monterey (Darwin 21) there is no known way to enable DND via system defaults
155169 await retryOnError ( ( ) => runAppleScript ( enableFocusModeAppleScript ) ) ;
156- } else {
170+ } else if ( platformMajor < 25 ) {
157171 const { stdout : locale } = await promisify ( exec ) ( getLocale ) ;
158172
159173 // From MacOS 13 Ventura (Darwin 22) there is no known way to enable DND via system settings
160174 await retryOnError ( ( ) =>
161- runAppleScript ( enableFocusModeVenturaAppleScript ( locale , platformMajor ) )
175+ runAppleScript ( enableFocusModeVenturaAppleScript ( locale ) )
176+ ) ;
177+ } else {
178+ const { stdout : locale } = await promisify ( exec ) ( getLocale ) ;
179+
180+ // From MacOS 26 Tahoe (Darwin 25) there is no known way to enable DND via system settings
181+ await retryOnError ( ( ) =>
182+ runAppleScript ( enableFocusModeTahoeAppleScript ( locale ) , {
183+ wrapped : false ,
184+ } )
162185 ) ;
163186 }
164187 } catch ( e ) {
0 commit comments