Skip to content

Convert Vcards to Mutt Alias

I love the Mac addressbook, its search functionality and its approach for organizing contacts into groups. But I use Mutt on several different computers for my power-emailing, and my Mutt Alias files are frequently out of sync. What to do?

One approach is to use the LBDB database package to enable mutt to query the Apple address book directly. That would ordinarily be useful, but doesn't help me when I'm using my non-primary computer. I decided to hack up a perl script that would take Vcards from the address book and export them to Mutt's alias format.

Easier said than done! What I came up with is v2m, a little perl script that might be useful to some. To use it, first select the contacts in your address book you'd like to add to your mutt alias file and export them to vcard. Make sure to check your preferences in Mac's addressbook to ensure you are exporting to latin-1 or iso-8859-1 format. Then use this perl script on that file, redirecting output to another file, as follows: v2m file.vcard > muttalias. The resulting file, muttalias, will still need minor editing but will otherwise be useable from within mutt.

I'm no perl hacker, so this script has some issues. Perl is no fan of non-Latin characters, first of all, and the Mac address book reverts to Apple's code page for encoding the vcards or UTF16, depending on what it finds (very clever, but annoying for this exercise!). I offer you no tech support in its use, but would gratefully accept edits, additions, and enhancements from those who know perl better than I do. As such, this software is available to you under the GPL v2 license.

Download v2m here or copy and paste the following (syntax highlighting courtesy of vim 6.2).

#!/usr/bin/perl -Tw

# V2M - Vcard to Mutt utility 
# Randall Wood July 2007 

# This perl script reads in a vcard file generated by Apple's
# Addressbook (and hopefully other Vcard files as well, though frankly
# I can't be bothered to check) and converts them into a Mutt alias
# file.  If you have an addressbook that contains diacritical marks
# for European languages Addressbook will produce a Vcard file in UTF8
# format; otherwise it will use ISO-8859-1.  a mutt alias file looks
# like the following: alias First_LastName Whatever the Name Is
# <address@example.foobar> alias Randall_Wood Randall Wood
# <address@example.foobar.org> This is my first perl script ever; if
# you find a better way of doing things, let me know via the email
# address at my website www.therandymon.com

# Thanks to Peter Watkins for the tip about using binmode to deal with
# UTF8 issues. 


# binmode ( STDIN, ':utf16' );  
# binmode ( STDOUT, ':encoding(iso-8859-1)' );

 my $mail; # create a couple of variables 
 my $dashname; # we'll need later
 my $realname;
 
# use open IN => ':encoding(utf16)'; # Default all open ()s to UTF16
# use open ':std'; #STDIN too, in case the script is pipelined

while ( <> )
{

if  (/^FN:(.*)/i)         
# find a name - everything after FN:
        {
s/FN://g;                 # lop off the FN: at the beginning
s/\s$//g;                 # lop any extra blank spaces at the end
$realname = $_;           # store it in a variable

s/\s/_/g;                 
# replace spaces with underscores _
        $dashname = $_;
# store it in a variable
        print "alias $dashname $realname ";
#print name but not email address
        }

        elsif (/:{1}([^\s()\[\]{}@,]*   
                  @
                  [^\s()\[\]{}@,.]{1}
                  [^\s()\[\]{}@,]*
                  \.
                  [a-z0-9]{2,4})/xi)            # find an email address
        {
                $mail = $1;             # store it in a variable
                print "<$mail>\n";      # print it and a newline character
        }                               # repeat
}

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

The author does not allow comments to this entry