macOS Bookmarks

Hey Folks,

People who are interested in the structure of macOS aliases and bookmarks may find these articles illuminating.

And Howard’s Precize.app and alisma command line tool may be of use to some.

My question to you all is:

Do we have any AppleScriptObjC code that will resolve a base64 bookmark reference like Howard’s tools?

-Chris

It looks like he’s using an undocumented resource key, NSURLBookmarkAllPropertiesKey.

So assuming you have a Base-64 string of the bookmark (and I’m not at all sure where he’s getting that from), it’s just a matter of:

use scripting additions
use framework "Foundation"

set theString to "XYZ..." -- Base-64 string
set theData to current application's NSData's alloc()'s initWithBase64EncodedString:theString options:(current application's NSDataBase64DecodingIgnoreUnknownCharacters)
set theResult to current application's |NSURL|'s resourceValuesForKeys:{"NSURLBookmarkAllPropertiesKey"} fromBookmarkData:theData
theResult as record -- if you want a record instead of a dictionary

If you want to get it directly from a file, you can use:

use scripting additions
use framework "Foundation"

set posixPath to POSIX path of (choose file)
set anNSURL to current application's |NSURL|'s fileURLWithPath:posixPath
set theData to current application's |NSURL|'s bookmarkDataWithContentsOfURL:anNSURL |error|:(missing value)
set theResult to current application's |NSURL|'s resourceValuesForKeys:{"NSURLBookmarkAllPropertiesKey"} fromBookmarkData:theData
theResult as record -- if you want a record instead of a dictionary

It’s entirely possible that NSURLBookmarkAllPropertiesKey is dependent on OS version and/or disk format.

2 Likes

I’m writing a script that needs to get a path from a plist preference file, and the path is stored as a MacOS filesystem bookmark. @ShaneStanley’s code (first example above) seems to work, by changing the value of resourceValuesForKeys in the third line to {"NSURLBookmarkOriginalRelativePathKey"}. First of all, thank you so much Shane!

Secondly I have a question now we’re four years in the future from when this was written: Is there a simpler way of converting a Bookmark into a file path? I’ve never used ASObjC before, so while I figured out how to get the code to do what I want, I don’t really understand it. I was just wondering if AppleScript has been updated in the intervening years to deal with MacOS Bookmarks natively? Given the focus Apple has given to AppleScript of late, I assume the answer is ‘no’, but thought I’d check anyway. :sweat_smile:

Thanks all, and thanks again Shane for the code above – I’m so glad this is possible even if I don’t understand it!

No, there hasn’t been any change.

2 Likes