Sunday, April 21, 2019

#1 2019-02-17 03:20:11 pm

akim
Member
Registered: 2010-04-04
Posts: 95

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

In an attempt  to create an NSAlert, my script

Applescript:

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

NSWindow drag regions should only be invalidated on the Main Thread!


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

Applescript:

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

fails.
How can I correct this script?

Offline

 

#2 2019-02-17 04:52:48 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5626

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

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 Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2019-02-17 09:04:48 pm

akim
Member
Registered: 2010-04-04
Posts: 95

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

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

Applescript:


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

The variable theAlert is not defined.


Could my script's problem be in any or in all of the following:
1.    My script is wrong
2.    The method

performSelectorOnMainThread

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

theAlert

3.    The previous error from the initial posting

NSWindow drag regions should only be invalidated on the Main Thread!

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.

Offline

 

#4 2019-02-17 09:13:47 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5626

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

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

Applescript:

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2019-02-17 11:38:53 pm

akim
Member
Registered: 2010-04-04
Posts: 95

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

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

performSelectorOnMainThread

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

Applescript:


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

performSelectorOnMainThread

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

its setMessageText:"This is an alert"
its setInformativeText:"Informatve Text"

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)