Retrieving bookmark data's last known location

Back before OS X 10.6, if you wanted to persistent a reference to a file or folder, the best way was to create an alias. However the code to do so lived in the crufty old Carbon Alias Manager. Several open source Cocoa wrappers sprung up around this (BDAlias and NDAlias come to mind). But it was still annoying to be having to use Carbon, particularly as error handling was fairly weak.

Fortunately in 10.6, Apple had a cleanup, and introduced the concept of bookmark data. As previously covered, moving from aliases to bookmarks, is fairly straightforward.

But we did lose one nice feature in the process. Should bookmark resolution fail, it's often handy to be able to pull out the last known location of the target to use as a starting point for the user to locate the missing item. With the old API, this was fairly straightforward to pluck out of the alias, using FSCopyAliasInfo().

When first shipped, bookmark data had no such equivalent. Generating a bookmark does allow you to include arbitrary values of your own choosing though. So if foresighted enough, you could deliberately ensure all your bookmarks are generated to include this extra bit of info. It's a bit galling though, to look at the raw data and see the path encoded in there, inaccessible to you!

Happily OS X 10.8 and iOS 6 brought with them the NSURLPathKey constant, which can be used to retrieve exactly what we want from bookmark data.

Annoyingly, you can't touch this constant on earlier OS releases, or it will crash your app. But you can dump out the raw value of it, and use that. Even better, it turns out the raw string works back to 10.7.4 at least!

Here's a quick example:

© Mike Abdullah 2007-2015