wangzhibo
2026-05-09 53418655c061ac26f66058253f39200aced1ab1d
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
import process from 'node:process';
import {promisify} from 'node:util';
import {execFile, execFileSync} from 'node:child_process';
 
const execFileAsync = promisify(execFile);
 
export async function runAppleScript(script, {humanReadableOutput = true, signal} = {}) {
    if (process.platform !== 'darwin') {
        throw new Error('macOS only');
    }
 
    const outputArguments = humanReadableOutput ? [] : ['-ss'];
 
    const execOptions = {};
    if (signal) {
        execOptions.signal = signal;
    }
 
    const {stdout} = await execFileAsync('osascript', ['-e', script, outputArguments], execOptions);
    return stdout.trim();
}
 
export function runAppleScriptSync(script, {humanReadableOutput = true} = {}) {
    if (process.platform !== 'darwin') {
        throw new Error('macOS only');
    }
 
    const outputArguments = humanReadableOutput ? [] : ['-ss'];
 
    const stdout = execFileSync('osascript', ['-e', script, ...outputArguments], {
        encoding: 'utf8',
        stdio: ['ignore', 'pipe', 'ignore'],
        timeout: 500,
    });
 
    return stdout.trim();
}