Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

If an error is returned from the AppleScript, then the offending code…

… is attached to the Error object as `err.appleScript`. Also directly including the raw stderr as the third parameter of the callback function, for legacy purposes.
  • Loading branch information...
commit cc50416bec0050185fec4217cb5124cb94bc79dd 1 parent fcd74e5
@TooTallNate authored
Showing with 13 additions and 13 deletions.
  1. +12 −12 lib/applescript.js
  2. +1 −1  samples/execString.js
View
24 lib/applescript.js
@@ -37,26 +37,20 @@ function runApplescript(strOrPath, args, callback) {
args.unshift("-ss"); // To output machine-readable text.
var interpreter = spawn(exports.osascript, args);
- var stdout = "";
- interpreter.stdout.on('data', function(data) {
- stdout += data;
- });
-
- var stderr = "";
- interpreter.stderr.on('data', function(data) {
- stderr += data;
- });
+ bufferBody(interpreter.stdout);
+ bufferBody(interpreter.stderr);
interpreter.on('exit', function(code) {
- var result = parse(stdout);
+ 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(stderr);
+ err = new Error(interpreter.stderr.body);
+ err.appleScript = strOrPath;
err.exitCode = code;
}
- callback(err, result);
+ callback(err, result, interpreter.stderr.body);
});
if (isString) {
@@ -65,3 +59,9 @@ function runApplescript(strOrPath, args, callback) {
interpreter.stdin.end();
}
}
+
+function bufferBody(stream) {
+ stream.body = "";
+ stream.setEncoding("utf8");
+ stream.on("data", function(chunk) { stream.body += chunk; });
+}
View
2  samples/execString.js
@@ -10,7 +10,7 @@ applescript.execString(script, function(err, rtn) {
throw err;
}
- if (Array.isArray(rtn)) {
+ if (Array.isArray(rtn) && rtn.length > 0) {
console.log("Currently selected tracks in \"iTunes\":");
rtn.forEach(function(songName) {
console.log("\t" + songName);
Please sign in to comment.
Something went wrong with that request. Please try again.