index.js 1006 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import process from 'node:process';
  2. import {promisify} from 'node:util';
  3. import {execFile, execFileSync} from 'node:child_process';
  4. const execFileAsync = promisify(execFile);
  5. export async function runAppleScript(script, {humanReadableOutput = true, signal} = {}) {
  6. if (process.platform !== 'darwin') {
  7. throw new Error('macOS only');
  8. }
  9. const outputArguments = humanReadableOutput ? [] : ['-ss'];
  10. const execOptions = {};
  11. if (signal) {
  12. execOptions.signal = signal;
  13. }
  14. const {stdout} = await execFileAsync('osascript', ['-e', script, outputArguments], execOptions);
  15. return stdout.trim();
  16. }
  17. export function runAppleScriptSync(script, {humanReadableOutput = true} = {}) {
  18. if (process.platform !== 'darwin') {
  19. throw new Error('macOS only');
  20. }
  21. const outputArguments = humanReadableOutput ? [] : ['-ss'];
  22. const stdout = execFileSync('osascript', ['-e', script, ...outputArguments], {
  23. encoding: 'utf8',
  24. stdio: ['ignore', 'pipe', 'ignore'],
  25. timeout: 500,
  26. });
  27. return stdout.trim();
  28. }