Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 40 additions & 34 deletions lib/applescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,52 @@ exports.execString = function execString(str, callback) {
}



function runApplescript(strOrPath, args, callback) {
var isString = false;
if (!Array.isArray(args)) {
callback = args;
args = [];
isString = true;
}
return new Promise((resolve, reject) => {
var isString = false;
if (!Array.isArray(args)) {
callback = args;
args = [];
isString = true;
}

// args get added in reverse order with 'unshift'
if (!isString) {
// The name of the file is the final arg if 'execFile' was called.
args.unshift(strOrPath);
}
args.unshift("-ss"); // To output machine-readable text.
var interpreter = spawn(exports.osascript, args);
// args get added in reverse order with 'unshift'
if (!isString) {
// The name of the file is the final arg if 'execFile' was called.
args.unshift(strOrPath);
}
args.unshift("-ss"); // To output machine-readable text.
var interpreter = spawn(exports.osascript, args);

bufferBody(interpreter.stdout);
bufferBody(interpreter.stderr);
bufferBody(interpreter.stdout);
bufferBody(interpreter.stderr);

interpreter.on('close', function(code) {
var result = parse(interpreter.stdout.body);
var err;
if (code) {
// If the exit code was something other than 0, we're gonna
// return an Error object.
err = new Error(interpreter.stderr.body);
err.appleScript = strOrPath;
err.exitCode = code;
}
if (callback) {
callback(err, result, interpreter.stderr.body);
interpreter.on("close", function(code) {
var result = parse(interpreter.stdout.body);
var err;
if (code) {
// If the exit code was something other than 0, we're gonna
// return an Error object.
err = new Error(interpreter.stderr.body);
err.appleScript = strOrPath;
err.exitCode = code;
}
if (callback) {
callback(err, result, interpreter.stderr.body);
}
if (err) {
reject(err);
} else {
resolve(result, interpreter.stderr.body);
}
});

if (isString) {
// Write the given applescript String to stdin if 'execString' was called.
interpreter.stdin.write(strOrPath);
interpreter.stdin.end();
}
});

if (isString) {
// Write the given applescript String to stdin if 'execString' was called.
interpreter.stdin.write(strOrPath);
interpreter.stdin.end();
}
}

function bufferBody(stream) {
Expand Down