Button activation in Xcode

Take the example of an application with a default OK button (whose keyEquivalent is return).
When I click this button, the action it triggers may take 5-7 seconds.
The button’s enabled state is set to false, the window is not dismissed (it’s a utility panel) and there’s a progress bar that starts to animate.

If I accidentally make one more click on the OK button, the action is triggered once again, right after it ends up.

Is there a way to really inactivate the button during the script process?

(I tried to set the action button to missing value, and set it back at the end of the script but the behavior remains the same.)

Any clue on this?

A binding is a possibility. But whatever you do, you also need to let the runloop clear any presses while your code does its thing, otherwise they just hang there, waiting until you’ve finished.

So change its enablement or action, then use something like -nextEventMatchingMask:untilDate:inMode:dequeue: to periodically handle the clicks until the other code is finished.

Old post

Binding was my first thought.
With my settings, the behavior is unchanged.

nextEventMatchingMask: I’m afraid I understand nothing (maybe yo could give me an example?)…
Or, is it possible to deactivate user interaction on the window’s view?

I think I’ve found a solution:
I’m using performSelector: withObject: afterDelay: to insert a tiny delay which forces the script to wait its completion in opposition of the setEnable: method.

The problem is once your action starts, any new events, like key presses, sit waiting in a queue, and are then handled when your process finishes (unless there are too many, in which case you see the spinning cursor). So what you need to do is regularly handle these events, so they don’t back up.

In ASObjC you can use a handler like this:

	on fordEvent()
		set theApp to current application's NSApp
		set theMode to current application's NSEventTrackingRunLoopMode
        set theMask to current application's NSDecimalNumber's decimalNumberWithString:"18446744073709551615"
		repeat
			set theEvent to (theApp's nextEventMatchingMask:theMask untilDate:(missing value) inMode:theMode dequeue:true)
			if theEvent is missing value then exit repeat
			theApp's sendEvent:theEvent
		end repeat
	end fordEvent

But for efficiency, you’re better running it in Objective-C if you can. If you download my Myriad Helpers, you’ll see a -fordEvent method in the ObjectWithFords class.

Great. I’ll give it a try.

For the moment, I’ve found a solution (see my amended post).

I’m already using your Myriad Helpers in a couple of apps for alerts as sheets.
And for this application, I’m planning to use the open dialog as sheet function.

There is so much in those helpers, I can’t find the time to learn every part.
Especially that some are hard concepts.

(By the way, some methods used in MH are marked deprecated. Is it a problem ?)

:wink:

It has the same effect, just a little more crudely.