Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ applescript.execString(script, (err, rtn) => {
(`*.applescript`) file as the first argument instead of the command itself, and you
may pass an optional Array of String arguments to send to the applescript file.

Each function takes an optional last parameter timeout (in milliseconds) to kill the
applescript process if it stops responding.

Licence
-------

Expand Down
17 changes: 12 additions & 5 deletions lib/applescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@ var parse = exports.Parsers.parse;
exports.osascript = "osascript";

// Execute a *.applescript file.
exports.execFile = function execFile(file, args, callback) {
exports.execFile = function execFile(file, args, callback, timeout) {
if (!Array.isArray(args)) {
callback = args;
args = [];
}
return runApplescript(file, args, callback);
return runApplescript(file, args, callback, timeout);
}

// Execute a String as AppleScript.
exports.execString = function execString(str, callback) {
return runApplescript(str, callback);
exports.execString = function execString(str, callback, timeout) {
return runApplescript(str, callback, timeout);
}



function runApplescript(strOrPath, args, callback) {
function runApplescript(strOrPath, args, callback, timeout) {
var isString = false;
if (!Array.isArray(args)) {
timeout = callback;
callback = args;
args = [];
isString = true;
Expand Down Expand Up @@ -60,6 +61,12 @@ function runApplescript(strOrPath, args, callback) {
interpreter.stdin.write(strOrPath);
interpreter.stdin.end();
}

if (timeout) {
setTimeout(function() {
if (interpreter.exitCode == null) interpreter.kill()
}, timeout);
}
}

function bufferBody(stream) {
Expand Down