JavaScript / JXA Q&A

Not sure if this is the latest version, but it covers the main ground:

https://developer.apple.com/library/mac/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/Debugger/Debugger.html

Obviously, there are times when debugging you do NOT want to allow the script to complete.
So, I guess my approach of closing the window is the only way to terminate the script when it is paused in Safari Debugger.

Thanks for reviewing my video and providing feedback.

However your psychoanalysis of the speaker is wrong and inappropriate. Just for your edification, "just" is used by many Texans to mean "simply". In the case of my video, when I said "Just close the window", a more complete, alternate statement would be "To terminate the debugging session, simply close the window."

Of course, and FWIW I absolutely agree that 'simply' works the same way as 'just'. We say 'simply close the window' rather than 'close the window' when we are vaguely aware that there may be complexities, but for the moment we're going to skip over them. Nothing psychoanalytic there – just the semi-conscious pragmatics of daily life.

(But I always pause when I hear myself say something like 'just' or 'simply', and ask myself what I'm trying not to think about :slight_smile: )

Perhaps that is how you think, but it is not how I and many others I talk to think.
"Simply close the window" means "it is very easy to accomplish this by closing the window". There are no expectations of any problems, vague or otherwise.

You're turning something very simple into something very complex by overthinking it.

I hope we have exhausted this largely irrelevant off-topic, as I'm sure everyone else is bored to tears by now. :smile:

After some more testing, closing the Safari Debugger window to terminate the session seems to work very well.

Back in Script Editor, make any change, compile, and run.
The Safari Debugger opens up fine. the key is doing a compile before running again.

Yep. Exactly how I do it. (Except I go back to Atom instead of Script Editor.)

If I want to run all the way through the script, then I do. Otherwise I simply, uh, just, uh… then I close the friggin’ window and I don’t care if it’s right or wrong. There. How’s that?

Since I already had most of the elements for this, I decided to put it all together in an Evernote Public Note, as much for myself as anyone. This is another one of those things that is reasonably easy to do/use, once you know how. The learning how part can be a bit tricky. So I hope this guide will help some.

Evernote Public Note
###[JXA] Debugging JXA Scripts with Safari Debugger
~~~ VER: 1.0 2016-06-20 ~~~

All constructive criticism is very welcome, including comments, error reports, suggestions for improvements.

Everybody can view this web page. Those with Evernote accounts can pull it into their account, although doing so means you have a separate copy, that will not update when/if I publish any updates.

I have added a Summary to the main post of this thread.

If I missed any key posts, or incorrectly labeled them, please post below and I will correct.

Of course, @DanThomas, you can revise anytime as you see fit.

1 Like

I have a problem with this. My script is meant to be run from the terminal and therefore begins with a shebang line. To run it under Script Editor I comment out that line. However, part of what I want from the script is for it to return a shell error code. So I import stdlib and use $.exit(). This works fine when the script is run from the shell with the shebang line restored, but $.exist() exits Script Editor when run from there. Since Script Editor doesn't allow stopping a script halted in the debugger and the debugger doesn't provide a way to end a script without playing it through or closing the window, I have been forced to continually reopen Script Editor each time I debug.

What I do is set a DEBUGGING flag at the top of the script then only do exit() when debugging is off, logging a message otherwise. Works, but sward. At least I catch all my error returns in one place.

I don't think this is correct.

Every time I have used the Safari Debugger from Script Editor, if I have paused the script in the Debugger, then closing the Debugger window will stop the script when it returns to Script Editor.

I have no need to close the Script Editor window or to restart SE.

  1. Put the following in Script Editor
  2. Compile
  3. Run
  4. Stops in Safari Debugger
  5. Close Safari window
  6. Script Editor exits
ObjC.import('stdlib');
function run(argv) {
    console.log('normal exit');
    debugger;
    $.exit(0)
}

What are you expecting this statement to do?
What is it's purpose?

As I said,

It doesn't stop, it runs to completion. Try:

console.log('start');
 debugger;
console.log('end');

closing the Safari window when it hits the debugger.

You are correct. I stand corrected.
The script continues executing in Script Editor AFTER the paused Debugger window is closed.

I believe this is a change in behavior.
Previously, the script did NOT continue running in SE.

Maybe a change/bug in Safari 10.0.3 (11602.4.8.0.1) on macOS 10.11.6?