@guidepup/setup
Version:
Setup your environment for screen-reader automation.
130 lines (114 loc) • 5.21 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.enableDoNotDisturb = enableDoNotDisturb;
const child_process_1 = require("child_process");
const os = __importStar(require("os"));
const errors_1 = require("../errors");
const runAppleScript_1 = require("./runAppleScript");
const util_1 = require("util");
const retryOnError_1 = require("./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) => {
// 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";
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 0.5
key up \\"c\\"
key up 63
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 checkbox 2 of group 1
set doNotDisturbCheckboxStatus to value of doNotDisturbCheckbox as boolean
tell doNotDisturbCheckbox
if doNotDisturbCheckboxStatus is false then
perform action 1 of doNotDisturbCheckbox
end if
end tell
end tell
end tell
end tell
-- Close Control Center drop down
tell application \\"System Events\\"
key down 63
key down \\"c\\"
delay 0.5
key up \\"c\\"
key up 63
end tell
"
my withTimeout(command, timeoutSeconds)
`;
};
async function enableDoNotDisturb() {
const platformMajor = Number(os.version().split("Version ")[1].split(".")[0]);
try {
if (platformMajor <= 20) {
await (0, util_1.promisify)(child_process_1.exec)(enableFocusModeShellscript);
}
else if (platformMajor === 21) {
// From MacOS 12 Monterey (Darwin 21) there is no known way to enable DND via system defaults
await (0, retryOnError_1.retryOnError)(() => (0, runAppleScript_1.runAppleScript)(enableFocusModeAppleScript));
}
else {
const { stdout: locale } = await (0, util_1.promisify)(child_process_1.exec)(getLocale);
// From MacOS 13 Ventura (Darwin 22) there is no known way to enable DND via system settings
await (0, retryOnError_1.retryOnError)(() => (0, runAppleScript_1.runAppleScript)(enableFocusModeVenturaAppleScript(locale)));
}
}
catch (e) {
throw new Error(`${errors_1.ERR_MACOS_FAILED_TO_ENABLE_DO_NOT_DISTURB}\n\n${e.message}`);
}
}