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.