If I create an AppleScript script called myscript.js and pass it to oascript it will execute the run function once and write "hello world" to standard data out:
function run(args) {
for (var i=0;i<10;i++) {
// out("number " + i); // execution error: Error on line 18: ReferenceError: Can't find variable: out (-2700)
}
return "hello world"; // written to standard data out
}
But if I want to write to standard data out multiple times, for example, in the for loop, how would I do this?
If I do the following it writes to standard error out multiple times and dispatches multiple events in the external application:
for (var i=0;i<10;i++) {
console.log("number " + i);
}
The only problem is that it's creating error events not standard data events.
In Script Editor the standard data out is sent to the results window. I want to print to the results window multiple times.
UPDATE:
It looks like it might not be possible. I found this quote here:
Q: My script will produce output over a long time. How do I read the results as they come in?
A: Again, the short answer is that you don’t — do shell script will not return until the command is done. In Unix terms, it cannot be used to create a pipe. What you can do, however, is to put the command into the background (see the next question), send its output to a file, and then read the file as it fills up.
Also, side note, if I want to use JavaScript instead of AppleScript should I be using cocoascript instead of osascript?