Wait on sheet to close

I am looking for a method to wait until a sheet is closed to continue running. With the following code, runFunctionY() will execute before my application receives a sender from the cancel or continue button. Instead I would like to have my application wait until the sheet sheetName is closed before processing runFunctionY().

on runApp_(sender)
	openSheet()
	runFunctionY()
end runApp_

on openSheet()
	current application's NSApp's beginSheet_modalForWindow_modalDelegate_didEndSelector_contextInfo_(sheetName, mainWindow, me, missing value, missing value)
end openSheet_

on cancelButtonClicked_(sender)
	current application's NSApp's endSheet_(sheetName)
	sheetName's orderOut_(sheetName)
end cancelButtonClicked_

on continueButtonClicked_(sender)
	current application's NSApp's endSheet_(sheetName)
	sheetName's orderOut_(sheetName)
	runFunctionX()
end continueButtonClicked_

I have tried adding a repeat statement to the code but this appears to stall any input. With the repeat statement in place the application stops receiving input and senders for the Cancel and Continue buttons are never received.

property sheetFinished : false
on openSheet()
	current application's NSApp's beginSheet_modalForWindow_modalDelegate_didEndSelector_contextInfo_(sheetName, mainWindow, me, missing value, missing value)
	repeat until sheetFinished
	end repeat
end openSheet_

on cancelButtonClicked_(sender)
	current application's NSApp's endSheet_(sheetName)
	sheetName's orderOut_(sheetName)
	set sheetFinished to true
end cancelButtonClicked_

on continueButtonClicked_(sender)
	current application's NSApp's endSheet_(sheetName)
	sheetName's orderOut_(sheetName)
	set sheetFinished to true
	runFunctionX()
end continueButtonClicked_

You have to move your call to runFunctionY() into one of the handler’s called after the sheet is dismissed. There is really no other way.

Thanks for the guidance Shane. I’ve modified my app like so to get the desired result. I don’t like having to move things out of the runApp_ function but with some extra comments it should be understandable.

property runAppActive : false

on runApp_(sender)
	set runAppActive to true
	openSheet()
	-- Continues with runFunctionY() if continue button from sheet is clicked. See "on continueButtonClicked_(sender)"
end runApp_

on openSheet()
	current application's NSApp's beginSheet_modalForWindow_modalDelegate_didEndSelector_contextInfo_(sheetName, mainWindow, me, missing value, missing value)
end openSheet_

on cancelButtonClicked_(sender)
	current application's NSApp's endSheet_(sheetName)
	sheetName's orderOut_(sheetName)
end cancelButtonClicked_

on continueButtonClicked_(sender)
	current application's NSApp's endSheet_(sheetName)
	sheetName's orderOut_(sheetName)
	runFunctionX()
	if runAppActive then
		runFunctionY()
	end if 
end continueButtonClicked_

on runFunctionY()
	--do things
	set runAppActive to false
end runFunctionY

If you are going to call asynchronous methods, you need to get used to it. (And please don’t call your handlers functions – it only causes confusion with real functions.)