Created
September 26, 2010 18:59
-
-
Save juanarzola/598211 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
Imports sender addresses from a Mail mailbox into AddressBook | |
Author: @ancientbuho | |
Asks you for: | |
- An Account and Mailbox where emails will be searched | |
- An Address Book Group name where the accounts will be imported (creates it if doesn't exists) | |
This can potentially take a long time depending your number of messages | |
*) | |
tell application "Mail" | |
set theAccountNames to the name of accounts | |
choose from list theAccountNames with prompt "Choose from what account to retrieve contacts:" | |
set theSelectedAccount to result as text | |
if theSelectedAccount is false then return | |
set theMailboxNames to the name of (mailboxes of account named theSelectedAccount) | |
choose from list theMailboxNames with prompt "Choose a mailbox in that account:" | |
set theSelectedMailbox to result as text | |
if theSelectedMailbox is false then return | |
set theImportedGroup to none | |
-- Choose a group to where to import contacts | |
tell application "Address Book" | |
activate | |
set theResult to display dialog "Choose an Address Book group in which the accounts will be imported" default answer "Imported from " & theSelectedMailbox | |
if button returned of theResult is "CANCEL" then return | |
set theInputGroupName to text returned of theResult | |
if not (group named theInputGroupName exists) then | |
set theImportedGroup to make new group with properties {name:theInputGroupName} | |
save | |
else | |
set theImportedGroup to group named theInputGroupName | |
end if | |
end tell | |
-- Do the import | |
tell application "Mail" | |
set theSelectedMessages to messages of mailbox theSelectedMailbox of account theSelectedAccount | |
set theMessageCount to count theSelectedMessages | |
set i to 0 | |
repeat with theMessage in theSelectedMessages | |
set thePerson to sender of theMessage | |
set theEmail to extract address from thePerson | |
set theName to extract name from thePerson | |
tell application "Address Book" | |
if not (person theName exists) then | |
set thePerson to make new person with properties {first name:theName} | |
(make new email at end of addresses of thePerson with properties {label:"work", value:theEmail}) | |
add thePerson to theImportedGroup | |
tell application "Address Book" to save | |
set i to i + 1 | |
end if | |
end tell | |
end repeat | |
if i = 0 then | |
tell application "Address Book" to display dialog "Did not find any new contacts to import from " & theSelectedMailbox | |
else | |
tell application "Address Book" to display dialog "Imported " & i & " contacts from " & theMessageCount & " messages" | |
end if | |
end tell | |
end tell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment