I have a small script made with JXA (JavaScript for Automation) that outputs a result to the shell with console.log.

However, console.log seems to output to stderr and I’d like to output to stdout. I can always 2>&1 when calling the script, but it would be preferable to output directly to stdout from the script.

Is there a way to accomplish that?

  • 1
    Close voters: This question on-topic since scripting is specifically listed as being on-topic on this site.
    – grg
    Feb 13, 2017 at 9:25
  • How are you writing to console.log surely you are writing to stdout or stderr
    – mmmmmm
    Feb 13, 2017 at 10:55
  • @Mark Just console.log('whatever'). Nothing out of the ordinary.
    – user137369
    Feb 13, 2017 at 23:06
  • Wgat does the documentation for console.log say?
    – mmmmmm
    Feb 13, 2017 at 23:14
  • @Mark I never found any official good documentation on JXA. You can’t really deduce this one, though. I don’t have a problem building scripts, I’m asking for the specific function (or option) that’ll do what I need.
    – user137369
    Feb 14, 2017 at 17:43

2 Answers 2


Found the solution: don’t use console.log or anything else. Instead of




It will be output to stdout.

  • Only returns last statement
    – basil
    Nov 11, 2018 at 7:21
  • @basil As would be expected.
    – user137369
    Nov 12, 2018 at 12:38
  • But what character encoding will be used?
    – hendrik
    Jun 27, 2019 at 9:27

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() {
    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.

  • 1
    Grate! But some data should be unwrapped before output: console.log(ObjC.unwrap(app.bundleIdentifier))
    – basil
    Nov 11, 2018 at 7:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .