views:

39

answers:

1

Is there a way to add an item that doesn't point to a file that exists on the file system to the "Open Recent" menu?

In an application not based on NSDocument, I can add an item to the "Open Recent" submenu with the following code:

[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:[NSURL URLWithString:stringToFilePath]];

It works as documented, as long as the URL points to a file that exists on the file system.

If the url doesn't point to a file on the system, such as a web url, or a custom url scheme, nothing happens.

For example, this code has no effect, and produce no log during execution, even if my app handles the scheme used in the URL:

[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:[NSURL URLWithString:@"http://www.stackoverflow.com"]];

Update: someone found (a long time ago) a way to tweak this menu to have it show files whether they exist or not: http://lists.apple.com/archives/cocoa-dev/2007/Apr/msg00651.html

I successfully managed to subclass NSDocumentController, but my override of the method - (NSArray *)recentDocumentURLs is never called.

It's not very surprising, as the doc says:

This method is not a good one to override since the internals of NSDocumentController do not generally use it.

But the doc doesn't say what to use instead and the poster didn't give more detail. Any idea?


If there is no solution, on workaround would be to rewrite the entire menu from scratch. If possible, I would prefer to avoid that, for all the stuff I get for free (like when you have two items with the same name, it displays the parent directory as well to help differentiate them).

A: 

It looks like you'll probably have to create your own menu and maintain your own separate list. This menu automatically excludes files that don't exist.

I believe this is also true of files on removable media that is absent (ie, if the media comes back, the I believe the file is once again available in the list if it hasn't been pushed off by more recent items).

Joshua Nozzi
I contacted the person who posted on the apple mailing list. Although he subclassed NSDocumentController, as recentDocumentURLs is never called, he ends up manually constructing the menu.
Guillaume