Add to the "Open Recent" menu an item that doesn't point to a file
Asked Answered
U

1

8

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).

Underclothing answered 13/7, 2010 at 11:8 Comment(0)
V
7

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).

Volturno answered 13/7, 2010 at 14:22 Comment(7)
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.Underclothing
Yeah, I also build a separate list and showed a similar menu for the user. In my case the list has mixed location types (local, url, cloud). I had to roll my own "open recent" for the case of local files, but I didn't manage to get the app to associate the files with their location - when saving, the "save as" dialog shows upGradual
Ok, meanwhile I have overridden saveDocument and call writeSafelyToUrl. But I think that now the app is not a Sandboxed app anymore, which would mean that it wown't get into the store?Gradual
@RaduSimionescu This should be posted as a separate question, not as comments.Volturno
How would you open local files urls from your own menu buttons, when the app needs to be sandboxed? I seem to get "Access to the path ... is denied".Gradual
ok I think I solved it. Here's what happens: if you manage your own local files URLs in a recent list, accessing the NSUrls constructed by you, are not accessible. BUT if you take them from NSDocumentController recentFilesList, they are accessible. The system is probably making some access exceptions only for the NSUrl objects from that list, even though your own NSUrls point to the same filesGradual
Yes, see "security scoped bookmarks" - It's the first "Important" box at almost the exact beginning of the File System Programming Guide ( developer.apple.com/library/mac/documentation/FileManagement/… ) and it points to the App Sandbox Design Guide ( developer.apple.com/library/mac/documentation/Security/… ).Volturno

© 2022 - 2024 — McMap. All rights reserved.