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?
osascript) and OSA-to-Unix (do shell script) communication. Your original question asks how to achieve the former—i.e. how to make an AS/JS shell process write data to stdout—whereas your newly added footnote is only relevant to the latter. e.g. In Node, you typically callconsole.log(…)to print a message to stdout andconsole.error(…)to print to stderr, or useprocessif you need greater control.standard data outuntil the command is done and throughreturn. I don't know if that is correct because if I am in Script Editor and I use console.log() and attach it to Safari, it sends output to the Safari console. But if I run it in my Adobe AIR console.log sends data tostandard error out. If Ireturna value from the script, that value is sent tostandard data out.#!/usr/bin/osascript -l JavaScriptat the top of your script, and run it as a Unix shell script. You can then useNSFileHandle.fileHandleWithStandardOutput().writeData(…)to write directly to stdout, bypassing the limitations imposed on OSA. But again, it’s a lot of hassle when the best answer is to avoid OSA completely, and use the right tool for the job (Python/Ruby/Node/etc) in the first place.NSis the traditional prefix for Cocoa class names as they appear in ObjC and most ObjC-bridged languages (Swift is an obvious exception). If you’re going to use Cocoa APIs then you really need to familiarize yourself with ObjC syntax before you can find your way around. If you don’t need to use Cocoa or Apple events, there’s no real value in JXA other than making your life difficult; either way there are much better options available: Swift for Cocoa development, AppleScript for desktop automation, Node for everything JavaScript.