Matt Rajca

AppKit Quick Tip: Populating the 'Open Recent' Menu in Non-Document-based Applications

July 23, 2011

While writing Nibble, my native Apple I emulator for Mac OS X and iOS, I realized an Open Recent menu would be beneficial to users who want to quickly open recently-loaded memory dumps. In standard Document-based applications, AppKit automatically registers newly-opened files as recents. Unfortunately, it doesn’t make much sense to make an emulator a Document-based application.

Luckily, even Non-Document-based applications contain an Open Recent menu, ready to be populated. Further, NSDocumentController makes it extremely easy to register recently-accessed files through its -noteNewRecentDocumentURL: method. AppKit will magically append the file at the passed URL to the Open Recent menu; it will even enable the Clear Menu item when necessary! When the user opens a recent file, AppKit will invoke the -application:openFile: method on NSApplication’s delegate object.

The complete code listing can be found below:

NSDocumentController *dc = [NSDocumentController sharedDocumentController];
[dc noteNewRecentDocumentURL:[NSURL fileURLWithPath:somePath]];


- (BOOL)application:(NSApplication *)sender
		   openFile:(NSString *)filename {
	
	// load the file
	
	return YES;
}