AppleScriptObjC throws “NSWindow should only be instantiated on the main thread!” in Script Editor, but works as an exported application

I’m trying to run an AppleScriptObjC script directly in Script Editor on macOS, but it fails with this error:

NSWindow should only be instantiated on the main thread!

The script uses AppKit to display an NSAlert containing an NSPopUpButton accessory view and three buttons. The intention is to let the user select one of five options and then choose an action: “Use existing”, “New”, or “Cancel”.

The relevant part of the script is:

use framework "AppKit"
use scripting additions

set alert to current application's NSAlert's alloc()'s init()
alert's setMessageText:"Choose an option"
alert's setInformativeText:"What would you like to do?"

set popup to current application's NSPopUpButton's alloc()'s initWithFrame:{{0, 0}, {250, 26}} pullsDown:false
popup's addItemsWithTitles:{"Option 1", "Option 2", "Option 3", "Option 4", "Option 5"}
alert's setAccessoryView:popup

alert's addButtonWithTitle:"Use existing"
alert's addButtonWithTitle:"New"
alert's addButtonWithTitle:"Cancel"

set response to (alert's runModal()) as integer

I investigated the thread on which the script runs:

use framework "Foundation"
use scripting additions

return (current application's NSThread's isMainThread()) as text

When run directly in Script Editor, this returns false.

However, when I export the original script as an application and launch that application, the interface works as expected.

How can I run this script form Script Editor? Is that even possible? It is a PITA to be forced to save as an application to test and debug!

It works that way because an application has a main thread that gets used by default, while in a script editor the editor itself is what is using the main thread to do its thing.

Script Editor has an option in the Script menu to run the script in the foreground, but in Script Debugger you need to explicitly perform a selector (handler) to do stuff on the main thread, for example:

if current application's NSThread's isMainThread() as boolean then
   doStuff()
else
   my performSelectorOnMainThread:"doStuff" withObject:(missing value) waitUntilDone:true
end if

to doStuff()
   -- whatever
end doStuff

Modal alerts have their own run loop, but note that UI objects usually get cleared when the application quits. In a script editor, UI objects created by a script are actually attached to the editor instance (that “current application” thing) - these items can be left dangling and no longer responsive if the script exits before cleaning up, since the connections (outlets, action handlers, etc) get broken when the script exits. In this case the editor can just be quit or restarted.

2 Likes

You mean “Run Application”?

Using that works, but then there is no output in the log?

Pressing the control key will show an alternate Run in Foreground menu item. Using the editor log from within the NSAlert can be a bit problematic though, since it doesn’t have access from the modal loop, but things like dialogs are still available.