File Screen

By the way this is the URL :
http://lists.apple.com/archives/AppleScript-Studio/2009/Oct/msg00003.html

Using the Objective-C class reported in previous link the NSAlert works nice.
Now I have a question: if I have a script and during a process there is an alert to send, how can I receive the result of the Button returned directly inside the same script that ask the NSAlert to show up (in this example “count123_”)?

Let’s say that I have a simple script like the one here:

--
property NSImage : class "NSImage"
property NSAlert : class "NSAlert"
property NSBeginAlertSheet : class "NSBeginAlertSheet"
script ErrorPanelAppDelegate
	property parent : class "MySuperClass"
	property MainWnd : missing value
	
	on applicationWillFinishLaunching_(aNotification)
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
	on count123_(sender)
		showAlert_(sender)

		(*
HERE I'D LIKE TO RECEIVE THE USER RESULT
		Like in the display dialog command, (and also in the Objective-C NSAlert command).
		if result is 1 then
		do something...
		else if result is 2 then
		do something else...
		end if
		*)

	end count123_

	on showAlert_(sender)
		set theAlert to current application's class "NSAlert"'s alertWithMessageText_defaultButton_alternateButton_otherButton_informativeTextWithFormat_("An alert", "OK", "Cancel", missing value, "Further explanation")
		theAlert's beginSheetModalForWindow_modalDelegate_didEndSelector_contextInfo_(MainWnd, me, "a:r:ci:", missing value)
	end showAlert_
	
	on alertDidClose_(what)
		log what
	end alertDidClose_
	
end script

And the class that receive the “a:r:ci:” didEndSelector is this one:

Thanks a lot.

Giovanni Medici

The short answer is that you can’t. It’s not a modal dialog. The call is asynchronous, so any code you insert there will probably run before the dialog has been dismissed. That’s what the alertDidClose_(what) handler is for – you handle the response there.