Skip to content

Commit 35b337b

Browse files
committed
temp: try to walk the control center looking for "Focus"
1 parent 61889f2 commit 35b337b

File tree

2 files changed

+96
-63
lines changed

2 files changed

+96
-63
lines changed

src/macOS/enableDoNotDisturb.ts

Lines changed: 83 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -37,68 +37,11 @@ set command to "
3737
my 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
10346
delay 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+
147161
export 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) {

src/macOS/runAppleScript.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ export const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;
55

66
export async function runAppleScript<T = string | void>(
77
script: string,
8-
{ timeout = DEFAULT_TIMEOUT } = { timeout: DEFAULT_TIMEOUT }
8+
{
9+
timeout = DEFAULT_TIMEOUT,
10+
wrapped = true,
11+
}: { timeout?: number; wrapped?: boolean } = {
12+
timeout: DEFAULT_TIMEOUT,
13+
wrapped: true,
14+
}
915
): Promise<T> {
1016
const scriptWithTimeout = `
1117
with timeout of ${timeout} seconds
@@ -38,7 +44,11 @@ end doWithTimeout
3844
{
3945
maxBuffer: DEFAULT_MAX_BUFFER,
4046
},
41-
(error, stdout) => {
47+
(error, stdout, stderr) => {
48+
console.log(error);
49+
console.log(stdout);
50+
console.log(stderr);
51+
4252
if (error) {
4353
return reject(error);
4454
}
@@ -51,7 +61,7 @@ end doWithTimeout
5161
}
5262
);
5363

54-
child.stdin.write(scriptWithTimeout);
64+
child.stdin.write(wrapped ? scriptWithTimeout : script);
5565
child.stdin.end();
5666
})) as unknown as T;
5767
}

0 commit comments

Comments
 (0)