Open/save/alert dialogs and sheets

I find myself using “choose file” and its relatives instead of the Cocoa methods because the Cocoa stuff is a bit laborious – lots of code, and lots of conversion between URLs/POSIX paths and aliases/files/HFS paths. But there’s no simple equivalent for sheets, which are fairly painful and require use of an Objective-C protocol and, in the case of open/save, a deprecated method.

So I’ve written some Objective-C categories to add some simpler methods to alerts, open/save panels, and windows. In the case of open/save panels, they also let you deal in HFS paths instead of NSURLs, if you wish.

A typical save sheet would be done something like this:

on showSavePanel_(sender)
set thePanel to current application's NSSavePanel's savePanel()
	tell thePanel
			setMessage_types_location_title_name_("Save the data:", {"txt"}, path to home folder, "My Save Title", "Some name")
			showOver_telling_selector_(mainWindow, me, "saveSheetDone:")
		end tell
end showSavePanel_
	
on saveSheetDone_(theResult)
		if theResult = missing value then
			-- cancel button
			log "Cancel pressed"
		else
			log theResult -- HFS path to file
		end if
end saveSheetDone_

You can still access the other settings via the Cocoa methods (like controlling whether invisible files appear), the location argument can be an alias, a file, or a path of either persuasion, and the callback handler gets sent the file path (or NSURL if you wish), rather than a return code that requires you to ask the panel for its URL.

There’s also a simple command to run them modally, again returning paths.

Before I release them, I’m looking for some volunteers to give them a try. They seem to work OK – they’re not particularly complex – but I’m also interested in feedback on the terminology of the methods, and whether I should add other properties to the setMessage_… method; there’s a compromise between simplicity and encompassing everything.

Anyone interested, drop me an email and I’ll send them in a sample project. Using them in your own is just a matter of adding the relevant files to your projects.

Hi,

I used the sheet examples from your book and it is a fairly painless but tedious process, but why there should be the need to do so much effort for what ought to be such a regularly used feature is beyond me.

That’s lower level code for you – more options, more control, more code…