Address Book scripts, part 2
February 14, 2007 at 1:54 PM by Dr. Drang
By reusing some of the code discussed in this post, I put together this AppleScript/bash script combination that searches the Address Book for a particular contact and opens that entry.
The AppleScript is called abid.scpt
. It’s stored in my ~/bin
folder looks like this:
on searchName(theName)
tell application "Address Book"
set nameWords to every word in theName
set personList to every person whose name¬
contains the first item of nameWords
repeat
if the length of nameWords = 1 then
exit repeat
else
set newPersonList to {}
set nameWords to rest of nameWords
repeat with p in personList
if name of p contains first item of nameWords then
set end of newPersonList to p
end if
end repeat
set personList to newPersonList
end if
end repeat
return the first item of personList
end tell
end searchName
on run argv
try
set contact to searchName(item 1 of argv)
return id of contact
end try
end run
It uses the same searchName
function discussed in the earlier post and, when run from the shell, returns the id for the contact. The id is a long string of hex characters followed by “:ABPerson.” Here’s the id for Apple in my Address Book:
A1A2AA41-FA30-40AE-9925-FD6DB270B0A5:ABPerson
What good is this? Well, it turns out this is most of the Uniform Resource Identifier (URI) for that entry, and if I issue this call from the Terminal
open addressbook://A1A2AA41-FA30-40AE-9925-FD6DB270B0A5:ABPerson
Address Book will open to the Apple Computer entry (my Address Book doesn’t reflect the recent name change to Apple, Inc.). With this knowledge, I made the following short shell script, called abopen
:
#!/bin/bash
uri="addressbook://"`osascript ~/bin/abid.scpt "$*"`
open $uri
This script is also saved in ~/bin
and is executable.
You’re probably wondering about the value of such a script. Wouldn’t it be easier—or, at least, no more difficult—to type “apple” in Address Book’s search field than issue an abopen apple
command from within the Terminal? Yes, of course, but that’s not how I use abopen
. Instead, I’ve used it to make a Contextual Menu item that allows me to select a name in any program—on a web page, in the body of an email message, in the text of a letter or report—then right-click (or Control-click) on that selection and choose “Open in Address Book” from the Contextual Menu. Address Book then opens to the entry with that name.
I used the very nifty On My Command and its companion program, OMCEdit to make the Contextual Menu item, and the process could not possibly have been easier. Here’s a screenshot of OMCEdit with everything needed to make the CM item.
The only things I had to do were:
- give the menu item a name;
- say that it was going to be a First Level item in the CM;
- say that it was going to be active on Selected Text; and
- enter the command just as I would at the Terminal, but with the special variable
__OBJ_TEXT__
to represent the selected text.
Everything else stayed at their default settings. I had never used On My Command before, but it only took me a minute or two to make this really useful CM item.
Now when I get an email telling me to set up a meeting with Sam Johnson, I just swipe through his name, select “Open in Address Book” from the CM, and Address Book pops forward with Sam’s contact info. It’s not as though this saves me a great deal of time over my old method—swipe, copy, bring up Address Book, click in search field, paste, return—but it feels so much better. It feels like the computer is working for me instead of me working for it.
What I really like about On My Command is how it leverages the old Unix tools idea and makes it almost effortless to put those tool in a GUI context. (That’s what I like about TextMate, too.) I expect to be playing with On My Command a lot in the next few weeks.