From 76a285ac3f30a9ae48fda8370b0410d7ccffe2a3 Mon Sep 17 00:00:00 2001 From: James Bunton Date: Sun, 30 Mar 2008 17:18:01 +1100 Subject: [PATCH] OSX Address Book to Mutt aliases --- mail/abook2mutt/Makefile | 10 ++++++++ mail/abook2mutt/ab2mutt.m | 49 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 mail/abook2mutt/Makefile create mode 100644 mail/abook2mutt/ab2mutt.m diff --git a/mail/abook2mutt/Makefile b/mail/abook2mutt/Makefile new file mode 100644 index 0000000..43623bc --- /dev/null +++ b/mail/abook2mutt/Makefile @@ -0,0 +1,10 @@ +CFLAGS := -Wall -W $(CFLAGS) +FRAMEWORKS := -std=c99 -framework Foundation -framework AddressBook + +ab2mutt: ab2mutt.m + $(CC) $< -o $@ $(CFLAGS) $(FRAMEWORKS) + +clean: + rm -f ab2mutt + +.PHONY: clean diff --git a/mail/abook2mutt/ab2mutt.m b/mail/abook2mutt/ab2mutt.m new file mode 100644 index 0000000..42e33ef --- /dev/null +++ b/mail/abook2mutt/ab2mutt.m @@ -0,0 +1,49 @@ +/* Copyright 2008 James Bunton + * Licensed for distribution under the GPL version 2. + * + * Dump the contents of the OSX address book in a format usable as by Mutt as + * an alias file. + */ + + +#import +#import + +#define NSPrint(fmt, ...) [[NSString stringWithFormat:fmt, ## __VA_ARGS__] writeToFile:@"/dev/stdout" atomically:NO encoding:NSUTF8StringEncoding error:nil] + +int +main() +{ + NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + + for(ABPerson* person in [[ABAddressBook sharedAddressBook] people]) { + NSString* firstName = [[person valueForProperty:kABFirstNameProperty] description]; + NSString* lastName = [[person valueForProperty:kABLastNameProperty] description]; + NSString* nickName = [[person valueForProperty:kABNicknameProperty] description]; + + if(!([firstName length] > 0 && [lastName length] > 0)) { + continue; // Ignore empty entries + } + + // Mutt requires a key + if([nickName length] > 0) { + NSPrint(@"alias %@ ", nickName); + } else { + NSPrint(@"alias %@%@ ", firstName, lastName); + } + + NSPrint(@"%@ %@ ", firstName, lastName); + + // WTF does ABMultiValue exist for? No fast enumeration? What's wrong with NSArray?! + ABMultiValue* emails = [person valueForProperty:kABEmailProperty]; + for(unsigned int i = 0; i < [emails count]; ++i) { + NSPrint(@"%@ ", [emails valueAtIndex:i]); + } + NSPrint(@"\n"); + } + + [pool release]; + + return 0; +} + -- 2.39.2