O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Mac OS X Panther Hacks
By Rael Dornfest, James Duncan Davidson
June 2004
More Info

HACK
#21
Hack Your Address Book with Perl
Perl, with a little glue, can interact directly with Mac's built-in applications' databases
The Code
[Discuss (0) | Link to this hack]

The Code

Through the wonders of Perl and the Mac::Glue module, this script adds an entry for Stevie Wonder to your Address Book.

TIP

You'll need to have the Mac::Glue Perl module installed before this script will work. For more on Mac::Glue, read .

Save the script as addressbook.pl, wherever you please:

#!/usr/bin/perl
use strict;
use warnings;

use Mac::Glue ':all';

my $ab = new Mac::Glue 'Address Book';

my $person = $ab->make(new => 'person',
 with_properties => {
   first_name => 'Stevie',
   last_name => 'Wonder',
 }
);

$ab->make(new => 'email', at => location(end => $person->prop('emails')),
 with_properties => {
   value => 'swonder@example.com',
   label => 'work',
 }
);

$ab->make(new => 'phone', at => location(end => $person->prop('phones')),
 with_properties => {
   value => '555-123-4567',
   label => 'work',
 }
);

$ab->make(new => 'phone', at => location(end => $person->prop('phones')),
 with_properties => {
   value => '555-890-1234',
   label => 'home',
 }
);

$ab->make(new => 'address', at => location(end => $person->prop('addresses')),
 with_properties => {
   street => '123 Main St.',
   city => 'New York',
   state => 'NY',
   label => 'home',
 }
);

__END__

The general method is to call the $ab->make() function to create a new person. You can set the various properties of that person after the fact with statements like this:

$person->prop('job_title')->set(to => 'Musician');

However, it is easier to add the initial property values with the with_properties parameter, as shown in the first few lines of the script:

my $person = $ab->make(new => 'person',
 with_properties => {
   first_name => 'Stevie',
   last_name => 'Wonder',
 }
);

While you could guess some of the properties of the person class, you'll find a consummate list in the Address Book glue manpage by typing gluedoc "Address Book" on the command line —assuming, of course, you have Mac::Glue installed.

For elements of a person (also listed in the Address Book glue manpage), there may be multiple values: multiple addresses, email addresses, phone numbers, and so on. To create elements of this sort in the person's address record, use $ab->make( ) again, similar to how we used it to create a person:

$ab->make(new => 'phone', at => location(end => $person->prop('phones')),
 with_properties => {
   value => '555-123-4567',
   label => 'work',
 }
);

Note the addition of the location this time; this says to add the new element to the end of the person's existing list of phone numbers. The label property states which type of element (e.g., work, fax, home, cell) you're creating.


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.