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
#40
Interleave Mail and Pine
Use Mac OS X's Mail and Terminal-based Pine email applications interchangeably as the mood strikes
The Code
[Discuss (0) | Link to this hack]

The Code

Loosely based on an idea posted to Mac OS X Hints (http://www.macosxhints.com), the following script traverses your Mail mailboxes and creates a representative mailbox/folder hierarchy in Pine's preferred mail directory: Home/mail.

TIP

Mac OS X Hints's version didn't quite suit my needs, because it didn't take into account mailboxes within folders within folders and so on. Also, this version is a self-contained script that takes care of Inboxes, mailboxes/folders, and Sent Items in one fell swoop.

Nested folders (e.g., House→Home Repair) are represented as nested folders/directories, and the mailboxes they contain are represented as mailboxname.mbox (e.g., Home Repair.mbox). The Inboxes associated with each of your Mail accounts and the Sent Messages folder associated with the first account the script finds are also symlinked, so that your sent messages are also archived in a Mail-accessible location:

#!/usr/bin/perl

# Mail2Pine
# Author: Rael Dornfest <rael@oreilly.com>
# Version: 2003-04-14

# Creates symbolic links between Mac OS X's Mail.app inboxes and
# mailboxes and Pine's mail directory (~/mail) allowing you to interleave
# Mail.app and Pine usage as the mood strikes.

my $user = $ENV{USER};
my $account_dir = "/Users/$user/Library/Mail/";
my $mailbox_dir = "/Users/$user/Library/Mail/Mailboxes";
my $mail_dir = "/Users/$user/mail";

use File::Find;

# Make a pine mail directory
-d $mail_dir or mkdir $mail_dir, 0700;

# Find and symlink Mail.app's account inboxes to ~/mail
find(\&accounts, $account_dir);

sub accounts {
 $File::Find::name =~ m!(POP-[^\/]+)/INBOX.mbox/mbox$! or return 0;

 # Symlink the INBOX 
 -e "$mail_dir/INBOX-$1" 
 or symlink($File::Find::name, "$mail_dir/INBOX-$1");

 # Symlink the Sent Messages mailbox if first account found
 -e "$mail_dir/sent-mail"
 or `ln -s "$account_dir/$1/Sent Messages.mbox/mbox" "$mail_dir/sent-mail"`;
}

# Find and symlink Mail.app's mailboxes to ~/mail
find(\&mailboxes, $mailbox_dir);

sub mailboxes {
 $File::Find::name =~ /SKindex/ and return 0;

 my($path, $mbox) = $File::Find::name =~ 
 m!^$mailbox_dir/(.*/)?([^\/]+)\.mbox/mbox!;
 
 $mbox or return 0;

 # Perform a little cleanup on paths and mbox filenames
 $path =~ s!^/|/$!!g;
 $path =~ s/[^\w\/]/_/g;
 $mbox =~ s!^/|/$!!g;
 $mbox =~ s/\W/_/g;

 $File::Find::name =~ s! !\\ !g;

 # Uncomment this line if you want to see what's going on along the way
 #print "$File::Find::name ~/mail/$path/$mbox\n";

 `mkdir -p $mail_dir/$path`;
 -e "$mail_dir/$path/$mbox" 
 or `ln -s $File::Find::name $mail_dir/$path/$mbox.mbox`;
}


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.