Apple - Output JXA to stdout
Found the solution: don’t use console.log
or anything else. Instead of
console.log('something');
Simply
'something';
It will be output to stdout.
It may not be the prettiest solution ever, but I think your best bet is to use the Objective-C scripting bridge. The alternative would be to deal directly with the command line and printf
, which would probably be even messier.
Using the scripting bridge, you can re-assign console.log
to a custom function that writes to stdout
using NSFileHandle.fileHandleWithStandardOutput
. If you put this definition at the top of your program, you can then use this revised console.log
.
console.log = function() {
ObjC.import('Foundation');
for (argument of arguments) {
$.NSFileHandle.fileHandleWithStandardOutput.writeData($.NSString.alloc.initWithString(String(argument) + "\n").dataUsingEncoding($.NSNEXTSTEPStringEncoding));
}
}
Note that I've added a line break at the end of each string—you may want to customize this behavior to meet your needs.