On 20 Jul 2016, at 10:56 PM, Jacob M. Small <email@hidden> wrote:
I’m using Dialog Toolkit 1.0.3 by Shane Stanley. But I’m writing in Script Debugger 6, which is problematic because the Dialog Toolkit commands need to be run on the main thread, which Script Debugger 6 doesn’t seem to support yet.
So I’m trying to adapt the approach in Chapter 26 of Everyday AppleScriptObjC for ensuring that Dialog Toolkit’s handler that creates the NSAlert is always run on the main thread.
For this, I’m kind of bumbling around in the dark, as ASObjC is pretty new to me. I’ll paste the handler twice below, first prior to my edit and then with the edit suggested in Everyday AppleScriptObjC. My question is whether my modification will permanently remove the need for me to be concerned about using this library on the main thread, or if I need to find a different approach.
The modification is after the tell block for theAlert. I’ve just added its performSelectorOnMainThread:"displayAlert:" withObject:theAlert waitUntilDone:true within the handler. I’m asking because I don’t quite understand yet what this is doing, just following the book’s instructions.
You're on the right track, but there's a problem: performSelector... doesn't return a result. Without that, you don't know what button was pressed.
The solution is to (a) make returnCode a property, and (b) perform a whole handler using performSelector....
So with returnCode a property, you have a new handler like this:
-- this handler needs to be called on the main thread on showTheAlert:theAlert -- check we are running in foreground if not (current application's NSThread's isMainThread()) as boolean then error "This handler must be called on the main thread." from current application set my returnCode to theAlert's runModal() end showTheAlert:
And then you replace set returnCode to theAlert's runModal() with the following:
my performSelectorOnMainThread:"showTheAlert:" withObject:theAlert waitUntilDone:true
Unfortunately that's not quite the end of things -- in a couple of places the script calls fitToSize(), and that too needs to be run on the main thread.
The good news is that I have a new version that works exactly the same as the old version, but has the required changes. The bad news is that I haven't had time to finish testing it so I can post it. But if you (or anyone else) would like to test it, email me off-list.
|