Head off "application quit unexpectedly, reopen windows" dialog.

I have an Applescript that runs all the time processing files as they come in. It uses Applescript’s built-in progress dialog for UI.

Occasionally - generally at random intervals fairly far between - it crashes. I’ve pretty well given up on figuring out why it crashes. Sometimes it runs for months.

It’s easy to find heuristics to figure out if it’s not doing its job, so I just wrote another app to watch it and restart it if it’s crashed.

The problem with this is that when it’s crashed, it generally won’t quit nicely, so I force quit it. But following a force quit, if I launch it by applescript using

tell application "AppName" to activate

it launches crashed. That is, it shows it’s running in the Dock and its application process name shows up in System Events, but its UI doesn’t come up and it doesn’t do anything. Usually the line:

tell application "System Events" to application process processName

Returns the process, but when it launches crashed, that line times out.

If I double-click it in Finder or click on it in the Dock after force-quitting it, when it launches, MacOS pops up a dialog saying:

With buttons “Don’t Reopen” and “Reopen.”

If I click the “Reopen” button, then it launches crashed. If I click “Don’t Reopen,” it launches fine and opens a new UI window and runs great.

So it seems that when I launch it with

tell application "AppName" to activate

It’s never showing that dialog asking about reopening windows, and is presumably defaulting to trying to reopen the window, which just crashes the program right away. Probably something about the progress dialog is what’s crashing the script in the first place?

Anyway, does anyone know a way around this? Is there anything I can add to the Applescript as a preference to never try to reopen windows after a crash? Is there some different scripted way to open it that would tell MacOS not to try to reopen the window?

Or maybe I need to find a way to open it where it does show the dialog, and I need to use UI scripting to detect the dialog and actually click the “Close” button?

Unfortunately, it’s hard to test with, because I can’t trigger the crashes… if it didn’t crash and get force quit, then it opens fine with a simple “activate.” Even if I force quit it, if it wasn’t crashed when it was force quit, it opens fine with an “activate.”

And usually when it really did crash, I don’t have time to mess around with experimenting with it… we need it running again ASAP.

edit: clarify what’s a MacOS window, and make contents of that window exact.

You could display the dialog using the giving up and handling that result.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--display dialog
set buttonList to {¬
	("Cancel"), ¬
	("Copy"), ¬
	("Okay") ¬
		}
set DialogReply to display dialog ("dialogMessage") ¬
	with title ("dialogTitle") ¬
	buttons buttonList ¬
	default button 3 ¬
	cancel button 1 ¬
	hidden answer false ¬
	giving up after 5 ¬
	with icon note

--{button returned:"",  gave up:true}

if gave up of DialogReply then
	-- do the launch stuff
else
	set userChoice to button returned of DialogReply
	-- do what you want
end if

Sorry, I’m either not understanding your answer, or else I didn’t explain the problem clearly enough.

The problematic dialog here is the dialog automatically presented by MacOS when I launch the application that says the application crashed, and do I want to to reopen its windows? It’s not a dialog I’m making or that I want to exist at all, I have no control over it.

Furthermore, this dialog isn’t even presenting when I launch the application via Applescript… I’m just assuming that it’s defaulting to try to reopen the window, and that’s why my application is crashing on launch.

If you’re suggesting I replace my progress dialog in the application with one akin to the dialog you posted, I’m not sure how that would work. It’s a progress dialog that stays open all the time to show what the application’s doing. It never expects any user input and is always present… I don’t think there’s a way to use a “giving up after” with Applescript’s progress dialogs, and even if there was, we want this window to be ever-present. I suppose I could replace the use of a progress dialog with something like a “display dialog” with a “giving up after” in a loop to show what the program’s doing, and it’s true that probably wouldn’t crash, try to reopen on launch, or crash the program on launch. But a series of flashing “display dialogs” sounds like a nightmare of a UI, and I think it would be constantly stealing focus on the machine.

I think it’s difficult to give any kind of help without knowing all of the code so others can run the script on their system and try to identify the problems.

I’m happy to provide the code, but with associated library calls, it’s several thousand lines. And with the required dependencies (it queries API’s with credentials, calls functions from several librariees referring to them at a symlink path, refers to specific folders on a mounted NAS) nobody would be able to get it to run anyway.

And the crashing problem sometimes only comes up every few months. I doubt any forum member is going to keep my application running in the background for months waiting for it to crash to dig into this. With the code, I can not reproduce this issue myself. So it’s a case where it does not seem to me that having the code really helps anyone.

The question is about bypassing MacOS’s dialog asking to reopen windows after an application crashes. I assume everyone’s seen this window before. It looks like this:
https://www.dropbox.com/s/q5a2v84xwd7xqcb/reopenoptions.png?dl=0
I was hoping that someone might have come across this before or have some input on that. I thought maybe one of the coders who does more ObjC or application development would know if MacOS has something like an application level setting to tell MacOS not to ask that, or a way to send a command to MacOS to reset whatever lets it know an application had crashed so the dialog would not show, or something along those lines.

It is an interesting question “What makes MacOS decide to show that dialog about reopening application windows?”

I thought maybe I could get to the bottom of this by reproducing the behavior with a simpler script.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions


-- Update the initial progress information

set nuSteps to 500
set progress total steps to nuSteps
set progress completed steps to 0
set progress description to "Processing Images..."
set progress additional description to "Preparing to process."

repeat with a from 1 to nuSteps
	
	-- Update the progress detail
	set progress additional description to "I'm making progress on nothing! Force Quit Me! I'm on step " & a & " out of " & nuSteps
	
	-- Process the image
	
	-- Increment the progress
	set progress completed steps to a
	
	-- Pause for demonstration purposes, so progress can be seen
	delay 1
end repeat

I tried saving that a stay-open Application and running it and force quitting it and relaunching it repeatedly, but I never get the dialog asking if I want to reopen windows.

It appears that the application has to actually be crashed at the time it’s force quit to get the dialog.

I don’t know how to reproduce the crash… or force some similar crash.

I’m very interested on what’s being set in MacOS and how it’s being set, that accounts for this difference. Why it is that I can display this progress dialog, force quit, relaunch, and I never get the MacOS dialog asking if I want to reopen windows, but in my actual app, if it’s crashed and I force quit, I do get that dialog when it’s restarted.