NSAlert's alloc()'s init() fails, please help

In an attempt to create an NSAlert, my script

use scripting additions
use framework "Foundation"
#    Framework AppKit is needed to  manipulate UIView
use framework "AppKit"

set theAlert to current application's NSAlert's alloc()'s init()

#    Set up alert
set theAlert to myAlert()

my performSelectorOnMainThread:"displayAlert:" withObject:{} waitUntilDone:true

on myAlert()
	tell current application's NSAlert's alloc's init()
		its setMessageText:"This is an alert"
		its setInformativeText:"Informatve Text"
		its runModal()
	end tell
end myAlert

on displayAlert:theAlert
     #    Display an alert as an app-modal dialog, and return a constant that identifies the button clicked.
	set buttonID to theAlert's runModal()
	if buttonID = (current application's NSAlertSecondButtonReturn) then error number -128
end displayAlert:

current application’s NSAlert’s alloc()'s init() failed resulting in the AppleScript Execution Error

I had the impression that the NSAlert instance is established an an NSObject, and then when needed to be viewed via the AppKit framework would be called by the performSelectorOnMainThread method to deliver the message to the main thread of this applescript.
For reasons, that I do not understand, the command

current application's NSAlert's alloc()'s init()

fails.
How can I correct this script?

Basically, the system has become fussier about what can be done on the main thread. Strictly speaking the creation of an alert should probably never have been allowed on a background thread, but it used to. The system seems to have become stricter in calling such things out.

Shane, thanks for your reply, but I have not been able to fathom a direction in which to proceed.

When I ran the following script


use scripting additions
use framework "Foundation"
use framework "AppKit"
property theAlert : missing value

set theAlert to my performSelectorOnMainThread:"createAlert:" withObject:(missing value) waitUntilDone:true
my performSelectorOnMainThread:"displayAlert:" withObject:theAlert waitUntilDone:true
on createAlert()
	#    Create an alert
	set theAlert to current application's NSAlert's alloc()'s init()
	my performSelectorOnMainThread:"displayAlert:" withObject:theAlert waitUntilDone:true
	tell current application's NSAlert's alloc's init()
		its setMessageText:"This is an alert"
		its setInformativeText:"Informatve Text"
		its runModal()
	end tell
end createAlert

on displayAlert:theAlert
	# Display an alert as an app-modal dialog, and return a constant that identifies the button clicked.
	set buttonID to theAlert's runModal()
	if buttonID = (current application's NSAlertSecondButtonReturn) then error number -128
end displayAlert:

the script erred with

Could my script’s problem be in any or in all of the following:

  1. My script is wrong
  2. The method

does not return any object, and hence will return no value to the variable named

  1. The previous error from the initial posting

referred to an error in the drag regions of the NSWindow class instance, and not to the NSAlert class instance.
4. I am invalidating an NSWindow instance’s drag regions with some background process, of which I am unaware
5. Some other issue, that I have not listed

Thanks for your help.

It’s mainly 1 and 2. here’s a working snippet:

use scripting additions
use framework "Foundation"
use framework "AppKit"
property theAlert : missing value
set theResult to missing value

my performSelectorOnMainThread:"createAlert" withObject:(missing value) waitUntilDone:true
my performSelectorOnMainThread:"displayAlert" withObject:(missing value) waitUntilDone:true
if theResult = (current application's NSAlertSecondButtonReturn) then error number -128

on createAlert()
	#    Create an alert
	set my theAlert to current application's NSAlert's alloc()'s init()
	my performSelectorOnMainThread:"displayAlert:" withObject:theAlert waitUntilDone:true
	tell theAlert
		its setMessageText:"This is an alert"
		its setInformativeText:"Informatve Text"
	end tell
end createAlert

on displayAlert()
	# Display an alert as an app-modal dialog, and return a constant that identifies the button clicked.
	set my theResult to theAlert's runModal()
end displayAlert

Thank you, Shane, for your insights.
With the instance method

used to:

  1. construct an alert
  2. display an alert
    the script now runs without returning an error.

on createAlert()
	set my theAlert to current application's NSAlert's alloc()'s init()

    #     I deleted the following line, as it appeared superfluous, and blocked assigning values to the NSAlert's message text and informative text.
	--my performSelectorOnMainThread:"displayAlert:" withObject:theAlert waitUntilDone:true
	tell theAlert
		its setMessageText:"This is an alert"
		its setInformativeText:"Informatve Text"
	end tell
end createAlert

After I deleted the third use of

as in the above script, the NSAlert instance populated its two defined attributes:

If by deleting that third instance method, I introduced risk of another error, please let me know.
In any event, I appreciate your help.