Internationalizing Numbers in Address Book

Theory on 2004-05-21T01:52:27

Anyone got a recipe to go through all of the phone nubmers in the Panther Address Book and add "+1 " to any numbers that don't have a + International code? Mac::Carbon, AppleScript, XML::Parser, or any approach would be welcome.

Thanks!

David


Attempt

pudge on 2004-05-25T23:39:55

use Mac::Glue;
 
my $ab = new Mac::Glue 'Address Book';
 
my $people = $ab->obj('people');
for my $person ($people->get) {
    for my $phone ($person->prop('phone')->get) {
        my $value  = $phone->prop('value');
        my $number = $value->get;
        if ($number =~ /whatever/) {
            $number = "whatever";
            $value->set(to => $number);
        }
    }
}
Not entirely tested, and it is incomplete, as I am not exactly sure how you want the number to be formatted. Just print "$number\n" after fetching to see all the values, and decide what you want to do with them from there.

Re:Attempt

Theory on 2004-05-26T00:43:48

pudge++

This did the trick for me. Thanks!

use Mac::Glue;

# sudo gluemac /Applications/Address\ Book.app

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

my $people = $ab->obj('people');
for my $person ($people->get) {
    for my $phone ($person->prop('phone')->get) {
        my $value  = $phone->prop('value');
        my $number = $value->get;
        next if $number =~ /^41-/ || $number =~ /^0/;
        next unless length $number > 9 && $number !~ /^\+/;
        if ($number =~ /(\d{3})[.-\s](\d{3})[.-\s](\d{4})(.*)/) {
            $number = "+1 ($1) $2-$3$4";
        } else {
            $number = "+1 $number";
        }
        print "$number\n";
        $value->set(to => $number);
    }
}

Re:Attempt

pudge on 2004-05-26T01:36:53

Great, glad it worked. I saw something about uninitialized values in IRC, did you figure that one out?

Re:Attempt

Theory on 2004-05-26T17:41:36

No, I just removed the -w from the shebang line. It was a one-off, and so not worth the effort to debug. Must've been a problem with the data in my address book...