wait for window to close before continuing execution

I have a project whose kernel runs in applicationWillFinishLaunching
The app starts does its job and then quits without user intervention.
It has one window only and it is hidden at start up.
The app does not need a window except for preferences.
If you press a modifier key at startup you can show the window, set preferences and dismiss it clicking on button save.
At present I tell it to quit and return.
How do I stop execution of code in applicationWillFinishLaunching until the preferences window is hidden by button save?
I would like to be able to pause while the window is visible (like in a modal dialog box) and resume execution when the window disappears.

This is what I do now:

on applicationWillFinishLaunching_(aNotification)
	if theFlag or firstRun then
		doPrefs_(me)
		set firstRun to false -- never do this again unless theFlag is true
		saveDefaults_(me)
		return
	else -- I would like to do your stuff after the window is closed and prefs are saved
		-- do your stuff
	end if
	saveDefaults_(me)
	quit
end applicationWillFinishLaunching_

on doPrefs_(sender)
	-- if logAll then log "starting doPrefs"
	-- show prefs window and allow setting preferences, takes the focus off the fields (maybe this is not needed
	-- used by: applicationWillFinishLaunching_
	-- uses:
	-- returns:
	aWindow's orderFront:me
	aWindow's makeFirstResponder:(missing value) -- take focus off the text field
end doPrefs_

on savePrefs_(sender)
	-- if logAll then log "starting savePrefs_(sender)"
	-- take focus off the fields, close window preferences, all values are saved by the bindings, quit
	-- used by: applicationWillFinishLaunching_
	-- uses:
	-- returns:
	aWindow's makeFirstResponder:(missing value) -- make sure editing is closed, for bindings etc.
	aWindow's orderOut_(me)
	quit -- this is what I would like to avoid
end savePrefs_

Look up runModalForWindow:.

Thanks Shane,

could I have said anything else instead of: current application’s NSApp’s stopModalWithCode_(0) ?
I tried other numbers as a parameter and they all worked, text did not.
The manual mentions stopModal() but I could not make it work.
Here it is:


script AppDelegate
	
	property parent : class "NSObject"
	
	property aWindow : missing value
	property theFlag : false
	property firstRun : true
	
	on applicationWillFinishLaunching_(aNotification)
		if theFlag or firstRun then
			doPrefs_(me)
			set firstRun to false -- never do this again unless theFlag is true
		end if
		doContinue()
		saveDefaults_(me)
		quit
	end applicationWillFinishLaunching_
	
	on hideModalWindow_(sender)
		current application's NSApp's stopModalWithCode_(0)
		aWindow's orderOut_(me)
	end hideModalWindow_
	
	on doContinue()
		log "continuing"
	end doContinue
	
	on saveDefaults_(sender)
		log "saving"
	end saveDefaults_
	
	on doPrefs_(sender)
		-- if logAll then log "starting doPrefs"
		-- show prefs window and allow setting preferences, takes the focus off the fields (maybe this is not needed
		-- used by: applicationWillFinishLaunching_
		-- uses:
		-- returns:
		current application's NSApp's runModalForWindow_(aWindow)
		-- aWindow's orderFront_(me) -- not necessary anymore
		aWindow's makeFirstResponder_(missing value) -- take focus off the text field
	end doPrefs_
	
	on savePrefs_(sender)
		-- if logAll then log "starting savePrefs_(sender)"
		-- take focus off the fields, close window preferences, all values are saved by the bindings, quit
		-- used by: applicationWillFinishLaunching_
		-- uses:
		-- returns:
		aWindow's makeFirstResponder_(missing value) -- make sure editing is closed, for bindings etc.
		current application's NSApp's stopModalWithCode_(0)
		aWindow's orderOut_(me)
	end savePrefs_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
end script