@guidepup/setup
Version:
Setup your environment for screen-reader automation.
50 lines (47 loc) • 1.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_MAX_BUFFER = exports.DEFAULT_TIMEOUT = void 0;
exports.runAppleScript = runAppleScript;
const child_process_1 = require("child_process");
exports.DEFAULT_TIMEOUT = 10000;
exports.DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;
async function runAppleScript(script, { timeout = exports.DEFAULT_TIMEOUT } = { timeout: exports.DEFAULT_TIMEOUT }) {
const scriptWithTimeout = `
with timeout of ${timeout} seconds
${script}
end timeout
on withTimeout(uiScript, timeoutSeconds)
set endDate to (current date) + timeoutSeconds
repeat
try
run script "tell application \\"System Events\\"
" & uiScript & "
end tell"
exit repeat
on error errorMessage
if ((current date) > endDate) then
error errorMessage & "\n\nFor script: " & uiScript
end if
end try
delay 0.2
end repeat
end doWithTimeout
`;
return (await new Promise((resolve, reject) => {
const child = (0, child_process_1.execFile)("/usr/bin/osascript", [], {
maxBuffer: exports.DEFAULT_MAX_BUFFER,
}, (error, stdout) => {
if (error) {
return reject(error);
}
if (!stdout) {
return resolve();
}
else {
return resolve(stdout.trim());
}
});
child.stdin.write(scriptWithTimeout);
child.stdin.end();
}));
}