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!