Skip to content

Commit 654ef3d

Browse files
committed
feat: update script with new tahoe position for checkbox
1 parent 174c7c3 commit 654ef3d

File tree

2 files changed

+12
-111
lines changed

2 files changed

+12
-111
lines changed

src/macOS/enableDoNotDisturb.ts

Lines changed: 10 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@ 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";
47+
// Tahoe moved the position of the DND checkbox in Control Center
48+
const position =
49+
platformMajor < 25 ? "checkbox 2 of group 1" : "checkbox 7 of group 1";
4450

4551
return `-- Startup delay to reduce chance of "Application isn't running (-600)" errors
4652
delay 1
@@ -61,7 +67,7 @@ set command to "
6167
tell its application process \\"Control ${center}\\"
6268
tell its window 1
6369
-- Check if Do Not Disturb already enabled
64-
set doNotDisturbCheckbox to checkbox 2 of group 1
70+
set doNotDisturbCheckbox to ${position}
6571
set doNotDisturbCheckboxStatus to value of doNotDisturbCheckbox as boolean
6672
6773
tell doNotDisturbCheckbox
@@ -87,98 +93,6 @@ my withTimeout(command, timeoutSeconds)
8793
`;
8894
};
8995

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: ")
102-
try
103-
log (" - name: " & (name of e))
104-
end try
105-
try
106-
log (" - role: " & (role of e))
107-
end try
108-
try
109-
log (" - description: " & (description of e))
110-
end try
111-
try
112-
log (" - value: " & (value of e))
113-
end try
114-
try
115-
log (" - help: " & (help of e))
116-
end try
117-
try
118-
log (" - title: " & (title of e))
119-
end try
120-
try
121-
log (" - identifier: " & (identifier of e))
122-
end try
123-
124-
if name of e is "Focus" then
125-
return e
126-
end if
127-
end try
128-
129-
try
130-
repeat with k in UI elements of e
131-
set found to my findDoNotDisturb(k)
132-
if found is not missing value then return found
133-
end repeat
134-
end try
135-
end tell
136-
137-
return missing value
138-
end findDoNotDisturb
139-
140-
-- Open Control Center drop down
141-
tell application "System Events"
142-
key down 63
143-
keystroke "c"
144-
delay 0.5
145-
key up "c"
146-
key up 63
147-
end tell
148-
149-
tell application "System Events"
150-
tell process "Control ${center}"
151-
set roots to UI elements of window 1
152-
153-
repeat with r in roots
154-
set doNotDisturbCheckbox to my findDoNotDisturb(r)
155-
if doNotDisturbCheckbox is not missing value then exit repeat
156-
end repeat
157-
158-
-- Toggle Do Not Disturb
159-
if doNotDisturbCheckbox is not missing value then
160-
set doNotDisturbCheckboxStatus to value of doNotDisturbCheckbox as boolean
161-
162-
tell doNotDisturbCheckbox
163-
if doNotDisturbCheckboxStatus is false then
164-
perform action 1 of doNotDisturbCheckbox
165-
end if
166-
end tell
167-
end if
168-
end tell
169-
end tell
170-
171-
-- Close Control Center drop down
172-
tell application "System Events"
173-
key down 63
174-
key down "c"
175-
delay 0.5
176-
key up "c"
177-
key up 63
178-
end tell
179-
`;
180-
};
181-
18296
export async function enableDoNotDisturb() {
18397
const platformMajor = Number(os.version().split("Version ")[1].split(".")[0]);
18498

@@ -188,21 +102,12 @@ export async function enableDoNotDisturb() {
188102
} else if (platformMajor === 21) {
189103
// From MacOS 12 Monterey (Darwin 21) there is no known way to enable DND via system defaults
190104
await retryOnError(() => runAppleScript(enableFocusModeAppleScript));
191-
} else if (platformMajor < 25) {
192-
const { stdout: locale } = await promisify(exec)(getLocale);
193-
194-
// From MacOS 13 Ventura (Darwin 22) there is no known way to enable DND via system settings
195-
await retryOnError(() =>
196-
runAppleScript(enableFocusModeVenturaAppleScript(locale))
197-
);
198105
} else {
199106
const { stdout: locale } = await promisify(exec)(getLocale);
200107

201-
// From MacOS 26 Tahoe (Darwin 25) there is no known way to enable DND via system settings
108+
// From MacOS 13 Ventura (Darwin 22) there is no known way to enable DND via system settings
202109
await retryOnError(() =>
203-
runAppleScript(enableFocusModeTahoeAppleScript(locale), {
204-
wrapped: false,
205-
})
110+
runAppleScript(enableFocusModeVenturaAppleScript(locale, platformMajor))
206111
);
207112
}
208113
} catch (e) {

src/macOS/runAppleScript.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@ 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 } = {
8+
{ timeout = DEFAULT_TIMEOUT } = {
129
timeout: DEFAULT_TIMEOUT,
13-
wrapped: true,
1410
}
1511
): Promise<T> {
1612
const scriptWithTimeout = `
@@ -61,7 +57,7 @@ end doWithTimeout
6157
}
6258
);
6359

64-
child.stdin.write(wrapped ? scriptWithTimeout : script);
60+
child.stdin.write(scriptWithTimeout);
6561
child.stdin.end();
6662
})) as unknown as T;
6763
}

0 commit comments

Comments
 (0)