Sandboxing Done Right - Recent Items

If any of you are using my old SandboxSafeRecentItems code, I would advise that you rip that code out, go into the back yard, shoot it, bury it, cover it in salt and burn it. While it does work, it’s an abomination that can cause applications to well up and die. Instead I’d recommend using my nice new shiny “Sandboxing Done Right - Recent Items” code.

Simple, you may or may not know, that I’ve spent literally months of hacking, swearing, cursing, digging and generally tearing Objective-C “Document based applications” apart (not to mention interrogating Apple’s engineers). I’ve done this to figure out why Obj-C Doc based apps are automagically Sandbox safe and why other apps have to hack, slash and suffer to work ‘alright’ (even Obj-C devs have to suffer when not using Doc based apps).

While there are several things that should be done, the easiest to implement is the NSDocumentController. When you use the NSDocumentController to manage the recent items list, the OS will then automatically maintain access to all the files in the list for you. Which means for recent items, no more SSBs, no more requesting & releasing access (before the OS does and then breaks your app).

Literally using the NSDocumentController makes a whole lot of Sandbox headaches go away and as a bonus, files in the Recent Items list also appear in the Dock Menu :slight_smile:

I’m including the full source code - for you to prod and poke at. So have fun and save money on aspirin or whatever else you take when your head hurts from banging it against a brick wall.

Wow! Thanks a lot. I’m sure it will spare me quite some headaches.

Autotuned, the months swearing might make a nice hip hop track. Congrats Sam!

Thanks Brad, I swear Sandboxing is the reason why my beard has gone grey!

Excellent & thank you!

Sam, you solved my problem before I even knew I had it. This would also make an excellent article for xDev magazine as an update to a recent items article of many years ago.

You’re both very welcome.

I’ll contact Marc and see if he’s interested.

Thank you. I really appreciate the code, learning alot.

Having some odd behavior with the demo and not sure how to change it. I dropped a couple .webloc files on the listbox and they showed up in all 3 places (listbox, file menu and dock menu). Selecting from the file menu triggers NewDocument but selecting from the dock menu launches the file in Safari.

Then I dropped in a .txt file and it only appeared in the listbox and file menu. After a quit and run the txt file appeared in the dock menu and selecting it triggered NewDocument. The webloc files still launch in Safari though.

Any ideas why the txt file didn’t appear in the dock at first and why webloc files get launched instead of triggering NewDocument?

(This is all from debug runs if that makes a difference)

Can’t figure this out.

Files are correctly getting added with noteNewRecentDocumentURL and retrieved with recentDocumentURLs, but the dock-menu’s list is only built the first time it’s shown. Thereafter it’s never updated until the next run. The file-menu’s list stays current.

What can be done to update the dock-menu’s list every time it’s shown? Is it my system: Xojo 13r2, OS 10.7.5, MacBook Pro late 2011?

Hi Doofus,
It’s an interesting situation.

The “Recent menu” in the menubar is created in Xojo (I haven’t found a way to get the OS to handle the menubar menu), so when you select a menu item from the Xojo menu, the folderitem is opened directly in the Xojo application. The dock menu is all handled by the OS.

I’m guessing here the OS is deciding that .webloc files should be opened in Safari, I’m wonder if you add a file type for .webloc and set up the application so that the app is an editor for .webloc, the OS might then open the files in the app.

As for the file not showing in the doc menu, again this is an OS level issue. I don’t recall seeing any functions for updating the menu when perusing the apple documentation, but then again the Doc menu was a nice surprise addition :slight_smile: It does seem to work here on 10.8.4

Yeah, that’s what I was hoping for. Been up and down the NS docs for some refresh menu method. I can’t find any mentions of noteNewRecent not working so I’m guessing the way Xojo manages the dock-menu is better handled in 10.8. Alas, I don’t plan to upgrade until 10.9 when I’ll put your code to use. Thanks again Sam for this.

This is fantastic! Thank you! I’ve successfully adapted this to suit my projects.

FYI, if you have macoslib in your project, in the “updateIcon” method you can add this code to have each file’s icon show up in the Open Recent menu:

me.Icon = GetFolderItem(me.Tag, FolderItem.PathTypeURL).Icon(16)

One thing that is bugging me is this – If there are two items in the recent file list with the same file name but exist in different paths, you can’t tell them apart. Xcode apps deal with it like this:

However, in your current implementation, you get this:

Any idea how to get the Xcode-like behaviour of displaying containing folders for identical filenames?

Am glad to hear it helped.

The short answer is not easily, I think you’ll have to attack it either with a custom view inserted into the menu item or maybe with a NSAttributedString. Both of which will require considerable work.

Wow! I just hit the “like” button in the OP. Unfortunately, I can’t find a “Love” button :wink:

I just went to your site. The link in the OP was a dead end. But I see it is available to buy as a plugin of some sort.

But on your site it I noticed it is fairly old. Will it still work with the current version of the Mac OS?
My guess is that it will. Unless lots of the Sandbox policies have changed over the last couple of years.

Download link is down (dead).

Any URL updates for it?