using multiple on dialog ended

My challenge is to use multiple on dialog ended handlers for the same window, with particular attention to the obvious fact that AppleScript will not allow me to have duplicate handlers.

My first approach was:


on displayDialog(theMsg, theButtonsArray, theDefaultButton, theGivingUp, theIcon, theWindow)
	
	script
		
		property itsMsg : theMsg
		property itsButtonsArray : theButtonsArray
		property itsDefButton : theDefaultButton
		property itsGiveUp : theGivingUp
		property itsIcon : theIcon
		property itsWindow : theWindow
		property dd : null
		property theResult : ""
		
		on showItself()
			set dd to display dialog itsMsg buttons itsButtonsArray default button itsDefButton ¬
				giving up after itsGiveUp with icon itsIcon attached to window itsWindow
		end showItself
		
		on dialog ended theObject with reply dd
			
			if (gave up of dd) then
				set theResult to itsDefButton
			else
				set theResult to (button returned of dd)
			end if
			
		end dialog ended
		
	end script
	
end displayDialog

-- for example:

set continueDlg to ¬
	displayDialog("You have not finished calculating your Spreadsheet." & return & ¬
		"Do you wish to continue calculating?" & return & return, ¬
		{"Yes", "No"}, "Yes", 15, "stop", mainWindow)
tell continueDlg to showItself()
		
set gSaved to (theResult of continueDlg) is "No"


However, when the compiled app is run, I get Error = -1708 which appears to be a generic catch-all error.

I could easily avoid the problem by not using on dialog ended, but call it an academic exercise.

Thanks bunches in advance for your help.

The embedded “dialog ended” handler won’t work, it’s not “visible” to the outside world (meaning your application).

Most dialogs are only informational. If you need to act upon entered data (like an input string), then you should consider making the dialog application modal (not attached to a window). That will cause your code to wait until the user dismisses the dialog box, then continue with the first statement after “display dialog.”

Edited after posting: I just realized that another (probably better idea) would be to create your OWN dialog panel in Interface Builder and then you can add your own buttons (which gives you an “on clicked” handler and you can sort actions based on button name).