Section 9.3. Address Book


9.3. Address Book

In the same spirit of standardization that inspired Apple to create Safari, Mac OS X includes the Address Book program built-in. As PIMs (Personal Information Managers) go, Address Book is hardly the most advanced. Still, Address Book works wonderfully with Mail, synchronizes with other Macs (if you've paid for the $99-a-year .Mac service, and, of course, Address Book is free.

What makes Address Book the darling of power users, however, is its AppleScript support. By sending simple commands to Address Book, you can:

  • Find every one of your friends who's employed by a particular company (Section 9.3.1).

  • Create your very own Smart Groups, to sort your contacts by certain criteria (Section 9.3.2).

  • Write your own Address Book plug-ins, to add extra features to Address Book's shortcut menus (see below).

Power Users' Clinic
Writing Address Book Plug-ins

Try this: In Address Book, choose a contact and click the word "home" or "work" next to his phone number. You'll see a pop-up menu listing a few actions Address Book can perform on the phone numberLarge Type (to display the number in monstrous digits) and Dial with Cell Phone (to automatically connect to the number using a Bluetooth-enabled cellphone), for example. This menu is a very powerful tool, but one overlooked by many beginners.

Even more powerful, however, is adding your own, customized actions. Address Book supports a special kind of plug-in for these menus, requiring no more than a few lines of AppleScript to create. Here's how you'd write a generic Address Book plug-in:

using terms from application ¬     "Address Book"     on action property         return phone     end action property     on action title for thePerson ¬         with phoneNumber         return [whatever you want to appear in the menu]     end action title     on should enable action for ¬         thePerson with phoneNumber         return true     end action title     on perform action for thePerson ¬         with phoneNumber         --this runs whenever you select the menu item     end perform action end using terms from

To make the script actually do something, you can replace the italicized part of the action title handler with, for example, "Copy Phone Number to Clipboard." Then you can replace the italicized comment in the perform action handler with a command like this:

set the clipboard to (the value of ¬     phoneNumber) as string

Save the script (with the File Format set to Script) in your Library Address Book Plug-Ins folder. Or, if you don't want other people who use your computer to run the script, save it in your Home Library Address Book Plug-Ins folder. (In either case, if the folder doesn't already exist, create it yourself.)

Once you restart Address Book, you'll see your new plug-in appear in every phone number's "home" or "work" menu. Simply click the Copy Phone Number to Clipboard command, and you'll automatically run your script's perform action handler. In other words, when you click the shortcut menu next to a phone number, the Copy Phone Number to Clipboard command does the same thing as selecting the phone number and choosing Edit Copy.

But feel free to replace the comment in the perform action handler with any AppleScript commands you want, to change how the plug-in works. And if you ever tire of writing Address Book plug-ins that simply work with phone numbers, www.macosxhints.com/article.php?story=2003110614140491 (at the bottom of the page) has an example of a more complex AppleScript plug-in that displays a map for a particular street address.


And of course, you can always link Address Book commands with those of other programs, to create powerful workflow-automation scripts like the one on Section 10.4.2.2.

9.3.1. Finding Contacts

At heart, Address Book is a database program. It's is great for storing contact information for your friends and business associates, and for keeping track of their birthdays, anniversaries, and more. When you add a new contact to your Address Book, that person's information gets stored in a simple database file. And when you click on someone's name, Address Book goes back into the database file, finds that person, and displays her information in Address Book's windowall without your ever seeing what's happening in the background.

If you use AppleScript, though, you can take this person-finding to a whole new level. For instance, if you want to display all your Address Book buddies who work for Umlaut Enterprises, you could use this script:

tell application "Address Book"     set thePeople to (the name of every person whose organization is ¬         "Umlaut Enterprises") --thePeople is now a list of employees' names end tell set selectedPerson to (choose from list thePeople) --Presents that list

On the other hand, that script is pretty strict; if one of your friends works for Umlaut Enterprises, LLC, your script won't find her, since her company isn't precisely "Umlaut Enterprises." To work around that problem, you can simply make this small tweak:

tell application "Address Book"     set thePeople to (the name of every person whose organization contains ¬         "Umlaut") end tell set selectedPerson to (choose from list thePeople)

Now your script will find all your friends who work for any company with the word "Umlaut" in its name. (Of course, you should replace "Umlaut" with the name of any company whose employees are in your Address Book.)

You can narrow down your list of contacts using other criteria, too: email address (to find all contacts with apple.com addresses), phone number (to find contacts only within your area code), or instant-messaging screen name (to get all contacts who use AOL Instant Messenger), for example. For a complete list of the possibilities, open Address Book's dictionary in Script Editor, and select Address Book Script Suite Classes.

9.3.2. Adding Contacts to a Group

Of course, the previous script gives you the same ability as Address Book's built-in search field (Edit Find Find, or -F). For true power, you can create the equivalent of iPhoto's smart albums feature (Sidebar 7.1), making entirely new groups to list contacts that match certain criteria. That way, you can keep a permanent list of all your friends who work for Umlaut Enterprises, for example, so you can email them as a group.

To create a new Address Book group from the result of your previous script, simply make the following tweaks:

tell application "Address Book"     --Part 1:     make new group with properties {name:"Umlauters"}     --Part 2:     add (every person whose organization contains "Umlaut") ¬         to group "Umlauters"     --Part 3:     save addressbook end tell

Here's how the script works:

  • Part 1 creates a new Address Book group named "Umlauters." Just like iTunes and iPhoto, Address Book lists groups in its left pane. (If you don't see a left pane, choose View Card and Columns, or press -1.)

For a refresher on the with properties option, see Section 5.3.1.1.

  • Part 2 finds all the employees of Umlaut Enterprises you have in your Address Book and then adds them to the group you created in part 1.

  • Part 3 is an unusual command, and one that's needed only in Address Book. If you left out this command, you wouldn't see any changes when you switched back to Address Bookat least, not until you restarted the program. However, since you do include this command, Address Book knows to update its window to show you the new group from part 1.

Now, if you wanted to send an email message to everyone who works for Umlaut Enterprises, you could simply create a new message in Mail and address it to "Umlauters". Then, Mail would take a look in Address Book, see that you have a group entitled Umlauters, and automatically substitute the email addresses of everyone who works for Umlaut Enterprises in the email message's "To:" field.



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net