-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathenableDoNotDisturb.ts
More file actions
121 lines (97 loc) · 3.94 KB
/
enableDoNotDisturb.ts
File metadata and controls
121 lines (97 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { exec } from "child_process";
import * as os from "os";
import { ERR_MACOS_FAILED_TO_ENABLE_DO_NOT_DISTURB } from "../errors";
import { runAppleScript } from "./runAppleScript";
import { promisify } from "util";
import { retryOnError } from "./retryOnError";
// REF: https://github.com/sindresorhus/do-not-disturb/issues/9
const enableFocusModeShellscript = `defaults write com.apple.ncprefs.plist dnd_prefs -data 62706C6973743030D60102030405060708080A08085B646E644D6972726F7265645F100F646E64446973706C6179536C6565705F101E72657065617465644661636574696D6543616C6C73427265616B73444E445875736572507265665E646E64446973706C61794C6F636B5F10136661636574696D6543616E427265616B444E44090808D30B0C0D070F1057656E61626C6564546461746556726561736F6E093341C2B41C4FC9D3891001080808152133545D6C828384858C9499A0A1AAACAD00000000000001010000000000000013000000000000000000000000000000AE && killall usernoted && killall ControlCenter`;
const getLocale = `defaults read -g AppleLocale`;
const enableFocusModeAppleScript = `
-- Startup delay to reduce chance of "Application isn't running (-600)" errors
delay 1
set timeoutSeconds to 30.0
set command to "
do shell script \\"open 'x-apple.systempreferences:com.apple.preference.notifications?focus'\\"
delay 5.0
tell application \\"System Preferences\\" to activate
delay 1.0
set doNotDisturbToggle to checkbox 1 of group 1 of tab group 1 of window \\"Notifications & Focus\\" of application process \\"System Preferences\\"
tell doNotDisturbToggle
if not (its value as boolean) then click doNotDisturbToggle
end tell
tell application \\"System Preferences\\" to quit
"
my withTimeout(command, timeoutSeconds)
`;
const enableFocusModeVenturaAppleScript = (
locale: string,
platformMajor: number
) => {
// TODO: attempt to rewrite scripts without any locale specific instructions
// so this setup can be used regardless of user locale preferences.
const center = locale.trim() === "en_GB" ? "Centre" : "Center";
const checkboxPosition =
platformMajor < 25 ? "checkbox 2 of group 1" : "checkbox 7 of group 1";
return `-- Startup delay to reduce chance of "Application isn't running (-600)" errors
delay 1
set timeoutSeconds to 30.0
set command to "
-- Open Control Center drop down
tell application \\"System Events\\"
key down 63
key down \\"c\\"
delay 1
key up \\"c\\"
key up 63
delay 2
end tell
tell application \\"System Events\\"
tell its application process \\"Control ${center}\\"
tell its window 1
-- Check if Do Not Disturb already enabled
set doNotDisturbCheckbox to ${checkboxPosition}
set doNotDisturbCheckboxStatus to value of doNotDisturbCheckbox as boolean
tell doNotDisturbCheckbox
if doNotDisturbCheckboxStatus is false then
click doNotDisturbCheckbox
end if
end tell
end tell
end tell
end tell
delay 2
-- Close Control Center drop down
tell application \\"System Events\\"
key down 63
key down \\"c\\"
delay 1
key up \\"c\\"
key up 63
delay 2
end tell
"
my withTimeout(command, timeoutSeconds)
`;
};
export async function enableDoNotDisturb() {
const platformMajor = Number(os.version().split("Version ")[1].split(".")[0]);
try {
if (platformMajor <= 20) {
await promisify(exec)(enableFocusModeShellscript);
} else if (platformMajor === 21) {
// From MacOS 12 Monterey (Darwin 21) there is no known way to enable DND via system defaults
await retryOnError(() => runAppleScript(enableFocusModeAppleScript));
} else {
const { stdout: locale } = await promisify(exec)(getLocale);
// From MacOS 13 Ventura (Darwin 22) there is no known way to enable DND via system settings
await retryOnError(() =>
runAppleScript(enableFocusModeVenturaAppleScript(locale, platformMajor))
);
}
} catch (e) {
throw new Error(
`${ERR_MACOS_FAILED_TO_ENABLE_DO_NOT_DISTURB}\n\n${e.message}`
);
}
}