Skip to content

Commit 4db04b5

Browse files
committed
Revert "temp: try to walk the control center looking for "Focus""
This reverts commit 35b337b.
1 parent 8b318ee commit 4db04b5

File tree

2 files changed

+63
-96
lines changed

2 files changed

+63
-96
lines changed

src/macOS/enableDoNotDisturb.ts

Lines changed: 60 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,68 @@ set command to "
3737
my withTimeout(command, timeoutSeconds)
3838
`;
3939

40-
const enableFocusModeVenturaAppleScript = (locale: string) => {
40+
const enableFocusModeVenturaAppleScript = (
41+
locale: string,
42+
platformMajor: number
43+
) => {
4144
// TODO: attempt to rewrite scripts without any locale specific instructions
4245
// so this setup can be used regardless of user locale preferences.
4346
const center = locale.trim() === "en_GB" ? "Centre" : "Center";
4447

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+
45102
return `-- Startup delay to reduce chance of "Application isn't running (-600)" errors
46103
delay 1
47104
@@ -87,77 +144,6 @@ my withTimeout(command, timeoutSeconds)
87144
`;
88145
};
89146

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) & " - " & (description of e) & " - " & (title of e) & " - " & (value of e) & " - " & (help 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 front window
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-
161147
export async function enableDoNotDisturb() {
162148
const platformMajor = Number(os.version().split("Version ")[1].split(".")[0]);
163149

@@ -167,21 +153,12 @@ export async function enableDoNotDisturb() {
167153
} else if (platformMajor === 21) {
168154
// From MacOS 12 Monterey (Darwin 21) there is no known way to enable DND via system defaults
169155
await retryOnError(() => runAppleScript(enableFocusModeAppleScript));
170-
} else if (platformMajor < 25) {
171-
const { stdout: locale } = await promisify(exec)(getLocale);
172-
173-
// From MacOS 13 Ventura (Darwin 22) there is no known way to enable DND via system settings
174-
await retryOnError(() =>
175-
runAppleScript(enableFocusModeVenturaAppleScript(locale))
176-
);
177156
} else {
178157
const { stdout: locale } = await promisify(exec)(getLocale);
179158

180-
// From MacOS 26 Tahoe (Darwin 25) there is no known way to enable DND via system settings
159+
// From MacOS 13 Ventura (Darwin 22) there is no known way to enable DND via system settings
181160
await retryOnError(() =>
182-
runAppleScript(enableFocusModeTahoeAppleScript(locale), {
183-
wrapped: false,
184-
})
161+
runAppleScript(enableFocusModeVenturaAppleScript(locale, platformMajor))
185162
);
186163
}
187164
} catch (e) {

src/macOS/runAppleScript.ts

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

66
export async function runAppleScript<T = string | void>(
77
script: string,
8-
{
9-
timeout = DEFAULT_TIMEOUT,
10-
wrapped = true,
11-
}: { timeout?: number; wrapped?: boolean } = {
12-
timeout: DEFAULT_TIMEOUT,
13-
wrapped: true,
14-
}
8+
{ timeout = DEFAULT_TIMEOUT } = { timeout: DEFAULT_TIMEOUT }
159
): Promise<T> {
1610
const scriptWithTimeout = `
1711
with timeout of ${timeout} seconds
@@ -44,11 +38,7 @@ end doWithTimeout
4438
{
4539
maxBuffer: DEFAULT_MAX_BUFFER,
4640
},
47-
(error, stdout, stderr) => {
48-
console.log(error);
49-
console.log(stdout);
50-
console.log(stderr);
51-
41+
(error, stdout) => {
5242
if (error) {
5343
return reject(error);
5444
}
@@ -61,7 +51,7 @@ end doWithTimeout
6151
}
6252
);
6353

64-
child.stdin.write(wrapped ? scriptWithTimeout : script);
54+
child.stdin.write(scriptWithTimeout);
6555
child.stdin.end();
6656
})) as unknown as T;
6757
}

0 commit comments

Comments
 (0)