Friday, January 18, 2013

Extract Emails from

I recently signed up for and wanted to configure a whitelist of email addresses for all my regular, and less-than regular, contacts. My email addresses are stored in OSX's, and visiting each contact record and cut-n-pasting email addresses out would be a ridiculously tedious effort. Luckily, being a programmer, I see this as an opportunity to develop a program to do the work for me!

Without further ado, here is a short Applescript to extract all email addresses from on Mac OSX 10.8+ and dump them to a new TextEdit file. It could be converted to earlier OSX versions without much difficulty probably, since I think the only difference is the Contact Manager app used to be called "Address Book" instead of "Contacts".

Here's the code:
set unique_emails to {}

tell application "Contacts"
 repeat with this_person in people
  repeat with this_email in every email of this_person
   copy the value of this_email & "
" to the end of unique_emails
  end repeat
 end repeat
end tell

tell application "TextEdit"
 make new document
 set theDate to current date
 set text of document 1 to unique_emails as text
end tell

One thing to note is that the copy the value of ... line literally includes a newline within the quotes. This causes an actual newline to be appended to every email address. If you would rather the email addresses be comma separated, you would use a comma here instead.

Have fun!