Address Book URLs

Last night, Brett Terpstra wrote a post about how to get links to Notational Velocity/nvALT notes into your Address Book via AppleScript. Since I don’t use either of the NVs, linking from Address Book to a note has no value for me, but I have needed to link to Address Book.

Brett’s links work because the NVs are registered to respond to the nv:// URL scheme. Similarly, the Address Book is registered to respond to addressbook:// URLs. A full URL to a particular contact in your Address Book will look something like this:


That happens to be the URL to the Apple, Inc. entry in my Address Book.

I’ve written about this a couple of times, and have incorporated links to contacts in PNotes, the no-server personal wiki I use to keep track of project notes at work.

The portion of the Address Book URL after the double slashes can’t, as far as I know, be extracted directly from Address Book itself, but it is accessible via AppleScript. Here’s a simple script to get the URL for a selected contact and put it on the clipboard.

1:  tell application "Address Book"
2:    set ABURLs to ""
3:    set contacts to the selection
4:    repeat with thisPerson in contacts
5:      set ABURLs to ABURLs & ("addressbook://" & id of thisPerson) & return
6:    end repeat
7:  end tell
8:  set the clipboard to text 1 thru -2 of ABURLs

If you have several contacts selected, it will return URLs to all of them, one per line.

The key to the script is Line 5, where the URL is constructed from the id property of the current contact. When the repeat loop is finished, there’s a trailing newline at the end of the ABURLs string. This is stripped just before the string is put on the clipboard in Line 8.

If you think there’s something funny about this line, you’re not the only one. As you can see, AppleScript, like many languages, can count back from the end of a string by using a negative number. The -2 in Line 8 refers to the “second to last” character in the string; -1 would refer to the last character. What I find odd about this is that other languages that use negative string indices—Perl, Python, and Ruby, for example—treat the index as an offset, with positive numbers (and zero) as offsets from the beginning of the string and negative numbers as offsets from the end. AppleScript, though, doesn’t use offsets in the forward direction; the first character in a string is character 1, not character 0. Why does it use offsets in the backward direction?

(Am I overthinking this? No doubt. I’m sure AppleScript’s designers would argue that you count characters the same way forward and backward but use negatives when counting backward. Still, if you’re used to the way character offsets work in other languages, AppleScript’s indexing seems inconsistent.)

Another thing that may strike you as weird is the as text at the end of Line 8. Isn’t ABURLs already text? Yes, it is, but

characters 1 thru -2 of ABURLs

is a list. The as text is necessary to turn that list back into text.

Update 2/4/12
Line 8 used to be

8:  set the clipboard to characters 1 thru -2 of ABURLs as text

which prompted the previous paragraph. But as Nathan Grigg points out in the comments, you can avoid the “string to list to string” conversion-go-round by using text 1 thru -2 instead of characters 1 thru -2. I’ve changed the script and struck out the paragraph that no longer fits.

The new Line 8 is still weird, though. The text 1 thru -2 construction for a substring is neither intuitive nor “English-like.”

So, once you have an Address Book URL, what can you do with it?1 In local web pages, like PNotes, you can link someone’s name to the corresponding Address Book entry:

<a href="addressbook://68A46B71-150A-4732-A183-D99EECCE1F18:ABPerson">Apple</a>

In scripts, you can use Apple’s open command to do the same thing:

open addressbook://68A46B71-150A-4732-A183-D99EECCE1F18:ABPerson

Unfortunately, OmniOutliner, an otherwise lovely program, doesn’t recognize Address Book URLs, so unlike http:// links, strings that start with addressbook:// don’t get automatically turned into clickable links. That’s something the OmniGroup could improve on.

Another unfortunate thing about Address Book URLs is that they don’t seem to be consistent across computers, even with iCloud syncing. The link I gave above for the Apple entry in my Address Book works only on my MacBook Air. On my iMac, that same entry has this link:


I assume the difference has something to do with the algorithm Address Book uses to generate an entry’s id. Maybe the id isn’t part of the package of data that gets synced, but is created on the fly as the entry is imported. Whatever the reason, it would be nice if the links were consistent across my computers.

  1. Does it seem like this post has an awful lot of questions? Are you tired of me using that rhetorical device? Me, too.