Enhanced Applet Crashes with display dialog

Hello, I have an applet using ApplescriptObjC and a simple subroutine of display dialog crashes the app when it is saved as an enhanced app. When its saved as a regular app it works. Any ideas?

on doDialog()
activate
display dialog “Test Text” buttons {“OK”} default button 1
end doDialog

I tried a simple test where I invoke display dialog from the run handler:

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

display dialog "Test Text" buttons {"OK"} default button 1

When saved as an Enhanced Applet (with and without the Show Startup Screen option checked) I don’t see a crash here.

Can you give is a complete example that illustrates the crash your are experiencing? Also, more information about the system (Mojave?) you are using would help. Finally, please capture a crash report and include that. The crash report may give a clue to the cause of the problem.

I am running High Sierra. Here is a handler I am calling from a NSMenuItem:

on actionHandler:sender
	set aTitle to title of sender as string
	
	if aTitle is equal to "Dialog" then
		activate
		display dialog "Test Text" buttons {"OK"} default button 1
	end if
end actionHandler:

I copied a portion of the error into this:
Crashed Thread: 1 Dispatch queue: NSOperationQueue 0x60c000422ea0 (QOS: UNSPECIFIED)

Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY

Application Specific Information:
Performing @selector(actionHandler:) from sender NSMenuItem 0x60c0000b3a40
*** Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘-[NSApplication runModalForWindow:] may only be invoked from the main thread. Behavior on other threads is undefined.’
abort() called
terminating with uncaught exception of type NSException

I got it to do the same thing with an applet I was modeling after. It crashes trying to show the dialog as an enhanced applet:

You can test it by copying answer #8

Please see the “Note For AppleScript Objective-C Users” section of the Enhanced Applet Release Notes. You must ensure that all UI related actions happen on the main thread.

Thanks. How come the limitation?

Running scripts on a background thread allows the Enhanced Applet UI to remain responsive even when scripts are blocked. This also allows scripts to run at full speed (i.e. no idle processing).

The main-thread requirement of UI-related ASObjC usage is imposed on us by AppKit. I’ve filed a bug at my end because I think we can probably improve our implementation of the various display xxx commands to avoid this particular crash.

You can also work around the issue by wrapping the dialog code in a tell application "System Events" block.

Thank you both for following up. It’s great you responded to “Why”. I tried system events and a display dialog worked in the first objC subroutine but not a second thread. Are there any limitations in an Applescript objC subroutine calling an applescript subroutine or do you think its the API limit? Its no biggie. More of an FYI. Probably cleaner not to have a souroutine calling another anyhow.

Ive tried to steer clear of system events display dialog as sometimes its hard to get back to the window and can be difficult for users when it happens.

We have addressed the display dialog crash in the upcoming Script Debugger 7.0.6 maintenance release. You can try a pre-release build here:

http://www.latenightsw.com/archives/beta_for_script_debugger/ScriptDebugger7.0.6-7A68.dmg

Note that this just fixes the crash calling display dialog (and other Standard Additions UI commands). All of the AppKit main-thread limitations remain.

The new update works great thanks!