Open and eneded panel questions

So I have this open panel that is created like this:

on chooseDefaultLocation()
	
	log "chooseDefaultLocation"
	set gettingFolder to true
	tell open panel
		set can choose directories to true
		set can choose files to false
		set prompt to "Choose"
	end tell
	display open panel attached to window "prefsWindow"
	
end chooseDefaultLocation

This panel is for choosing a default folder that contains files used in the rest of the solution. This works and when it ends the panel ended handler is called and life is good.

I now need another open panel that will look for files not folders when this is finished it needs to load a different variable than the previous one.

I call it in a similar manner:


on chooseFile()
	log "ChooseFile"
	
	set gettingFile to true
	tell open panel
		set can choose directories to false
		set can choose files to true
		set prompt to "Choose"
		activate
	end tell
	display open panel
end chooseFile

As you can see the first is attached to one of my windows and the second is free floating.

The first problem is, when this panel ends, the panel ended handler is not called.

The next part is that I am using another variable to branch the panel ended handler (this is the geetingFolder and gettingFile booleans. Is there a more elegant way for me to determine which panel has ended?

Dee

Jacques answered this.

If you have multiple uses for the same panel ended handler…

I’ve used something like this before:

global usingOpenPanelFor

on clicked theObject
	tell open panel to set allows multiple selection to false
	set usingOpenPanelFor to name of theObject
	
	if result is "OutputFolder" then
		tell open panel
			set can choose directories to true
			set can choose files to false
		end tell
	else
		tell open panel
			set can choose directories to false
			set can choose files to true
		end tell
	end if
	
	display open panel attached to window "preferences"
end clicked

on panel ended theObject with result withResult
	if withResult is 1 then
		set content of default entry usingOpenPanelFor of user defaults to (path name of open panel)
	end if
end panel ended

Now, in this project I’m using user defaults… If you want to set a global variable, the panel ended handler might look like this:

on panel ended theObject with result withResult
	if withResult is 1 then
		if usingOpenPanelFor is "OutputFolder" then
			set gettingFolder to path name of open panel
		else
			set gettingFile to path name of open panel
		end if
	end if
end panel ended