Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Mail Previous Recipients "port" to /folder/ in Contacts

i have been endlessly struggling with my various databases and i am not seeing what i think should be a natural integration between Mail Previous Recipients (Window > Previous Recipients) and a /folder/ in Mac Mail. right now this database resides /solely/ on one machine and i cannot get autocomplete for this data when writing emails on my other devices.


also, you cannot add these addresses in bulk (it will trash your database) and it appears to either over-write an existing phone number with a "new" same name contact with email address only or it create duplicate contacts (depending on whether you add them in bulk or add them individually).


ALSO, i am not exactly sure how you would MAINTAIN this database over time since if you manually enter the data into Contacts and then wipe the Previous Recipients clean - then ostensibly anytime you reply to an email address that already exists in a Contact database will get added to the previous recipients data in Mail and you will then have no way distinguishing between really new and new already existing email contacts.


so - can anyone code up a script that would let me simply add all the PREVIOUS RECIPIENTS data to a FOLDER in Contacts so that i can simply run it once in a while to update the data "port" to Contacts? if it resides simply in a folder named "previous recipients" this data will ostensibly be available to autocomplete on all my devices and it won't (i assume) clutter up or otherwise trash my existing Contacts database with all my phone numbers.


i have three or four hundred of these and while i can enter the data manually into my Contact database over time it would be a lot simpler to just have this data available in Contacts...


thank you for any consideration and for entertaining this request.


jon

Mac Pro, Mac OS X (10.6.8), 10.6.8 with 64 bit Win7 in Parallel

Posted on Aug 16, 2012 10:25 AM

Reply
Question marked as Best reply

Posted on Aug 16, 2012 1:08 PM

If you're seeking to maintain a list of recipients, then that tends to be a task that a customer relationship management (CRM) package would perform, or you might choose to use a package such as Apple's Address Book Server or equivalent, or (and this is less common) adding the target users into the LDAP directory.


If one of these existing approaches and databases cannot meet your requirements, then it would then appear that hiring a programmer to code the necessary scripts and probably a Cocoa tool might be the most expedient approach for you. (Importing data and updating the contents is typical of most database applications.)


I don't know that Apple documents and supports a way to extract the previous recipients for you; you'd end up with some code that accesses the ~/Library/Application Support/AddressBook/MailRecents-v4.abcdmr database (all of which would likely be undocumented and subject to change by Apple) and that then merges its contents with your preferred contact database using the Cocoa Address Book framework or whatever is appropriate for the target database.


The alternative being to add the users to the Address Book or LDAP directory database before sending the messages, rather than extracting the contact addresses from "underneath" Mail.app.

37 replies

Aug 28, 2012 5:30 PM in response to Pierre L.

hi pierre. thank you. sorry applescript is new to me and the app is a bit unpolished.


i run your original script on my mac pro (10.7.4) and i get contacts added to the group.


however, when i am on the macbook pro (10.8) if i change the first line to read "previous recipients_MBP" i get a dialog that says "0 contacts added" even though i have fifteen contacts in Window > Previous Recipients. i have tried to quit out of Applescript and quit out of Mail and quit out of Contacts and then start up Applescript and then i get the same error.


i have tried to change the first line to"previous recipients MBP" to take out the "_" (underscore) character and to add this folder to Contacts and i get the same issue where it says "0 contacts added".


I will try now by using the same "previous recipients" group when on the macbook pro but it seems like both of the groups above should work...


ideas on fixing/troubleshooting?


- jon


set theGroupName to "previous recipients MBP" -- or any other name that fits your needs



set theNewNames to {}

set theNewEmails to {}



tell application "Mail"


launch


activate

end tell



-- Get all the previous recipients:

tell application "System Events" to tell process "Mail"

click menu item "Previous Recipients" of menu 1 of menu bar item "Window" of menu bar 1


key code 51 -- to remove the whole contents of the search field


keystroketab


keystroke "a" usingcommand down-- select all the rows

tell rows of table 1 of scroll area 1 of window "Previous Recipients"

set theCardIcons to image 1

set theNames to value of text field 1

set theEmails to value of text field 2

end tell


keystroke "w" usingcommand down-- close the "Previous Recipients" window

end tell



-- Get only the previous recipients who are not already in your contacts:

repeat with k from 1 to (count theEmails)

if (item k of theCardIcons is missing value) then

copy (item k of theNames) to the end of theNewNames

copy (item k of theEmails) to the end of theNewEmails

end if

end repeat



-- Get the first and last names if possible -- that's the weakest part of the script

set theNewFirstNames to {}

set theNewLastNames to {}

repeat with thisName in theNewNames

set theFirstName to ""

set theLastName to ""

if (thisName is not "") and ("@" is not in thisName) then

try

set theLastName to word -1 of thisName

set theFirstName to text 1 through word -2 of thisName

end try

end if

copy theFirstName to the end of theNewFirstNames

copy theLastName to the end of theNewLastNames

end repeat



-- Add those previous recipients to the contacts, in the “Previous recipients” group:

tell application "Contacts"


launch


activate

set theGroup to grouptheGroupName

set theAlreadyAddedEmails to (value of email of people of theGroup) as text

set k to 0

repeat with j from 1 to (count theNewEmails)

set thisEmail to itemj of theNewEmails

if not (thisEmail is in theAlreadyAddedEmails) then

set thisFirstName to itemj of theNewFirstNames

set thisLastName to itemj of theNewLastNames

set thisPerson to makenewpersonwith properties ¬

{first name:thisFirstName, last name:thisLastName}


makenewemailat end of emails of thisPersonwith properties ¬

{label:"eMail", value:thisEmail}


addthisPersontotheGroup

set k to k + 1

end if

end repeat


save

set selected of theGroup to true


display dialog "Done (" & (k as text) & " contacts added)." buttons {"OK"} default button 1 with icon 1 giving up after 2

end tell

Aug 28, 2012 5:56 PM in response to hotwheels22

OK. i have tried a test.


the contacts that i wish to add from Mail > PREVIOUS RECIPIENTS in Mac Mail on my macbook pro are /not/ in the Contacts database. however, i have responded to a random email and this random email was added to the Mail > PREVIOUS RECIPIENTS list with the other addresses that i would like to add to this new group (thsi group is "Previous Recipients TEST").


the NEW email address was added but the ones that were already in this list were /not/ added to the group.


i think this must mean that either Mac Mail, the Applescript or Contacts is "hiding" these so they do not transfer to the group.


any ideas on fixing this pierre?


again apologies for this being long but it is a very worthwhile issue to solve and others will use it i am sure.


- jon

Aug 28, 2012 7:32 PM in response to hotwheels22

any ideas on fixing this pierre?


No, sorry.


The only thing I can do is try to explain once more, as precisely as I can, what the script does. The script:

• lists all the emails it finds in Mail's "Previous Recipients" window;

• removes from that list all those emails which are in rows beginning with a card icon, in other words all those contacts which are already in your Address Book;

• adds the rest of the emails to the "previous recipients" group already existing in your Address Book, provided they do not already exist in that group, in order not to create duplicates.

That' all!


On my MacBook Pro, under OS X 10.8, everything seems to work flawlessly, so that I really don't understand why it doesn't work on your MacBook Pro.


Sorry, Jon, but this post will be my very last one in this thread.


Message was edited by: Pierre L.

Aug 28, 2012 8:12 PM in response to Pierre L.

i think i see the problem.


i am needing a way to let the script add /ALL/ the contacts in Previous Recipients instead of removing the ones with the card symbol. since there is no way to remove this card icon in Mail (AFAIK), when you use the TEST GROUPS and then remove the contacts that were ported - Mail still thinks all these have been added.


also, since i am just dumping these into a Group to get access to autocomplete across all my devices it doesn't matter if there are duplicates that reside in this folder.


one last pass here Pierre?


anyone able to know if there is a way to edit this script to get it to the finish line?


THANKS

Aug 29, 2012 8:50 AM in response to Pierre L.

arg.


i have commented out this section (the script is very nicely annotated) in the hopes of adding Previous Recipients to the destination folder even if they appear to be duplicates but i am still getting a "zero contacts added" prompt after running it.


-- Get only the previous recipients who are not already in your contacts:

repeat with k from 1 to (count theEmails)

if (item k of theCardIcons is missing value) then

copy (item k of theNames) to the end of theNewNames

copy (item k of theEmails) to the end of theNewEmails

end if

end repeat


if i look at the list of Previous Recipients (Mail > Window > Previous Recipients) that i am /trying/ to add, and if i search for these names in the Contacts database i can see that these contacts do not exist in the Contacts database, even though there is a little card symbol next to them in Previous Recipients. and since these are all gettting added to a single folder simply to get autocomplete working for this data it is not necessary to "cull" duplicates.


anyone have an idea on how to get this working?


THANKS


Aug 29, 2012 1:47 PM in response to hotwheels22

OK. just got this working on another thread. and again a big thanks to pierre for this script.


for those following along later this will PORT your previous recipients to Address Book (10.7.4) or Contacts (10.8) and drop them in a folder that you have to name "previous recipients". if you are here you probably know how helpful that will be.


also, if you /lost/ your previous recipients you can find it in the MailRecent... file in Application Support > AddressBook.


not sure if it is pasting correctly below so here is the link: https://discussions.apple.com/thread/4250548?answerId=19418535022#19418535022&ac_cid=op123456#19418535

------------


settheGroupNameto "previous recipients" -- or any other name that fits your needs

tellapplication "Mail"

launch

activate

endtell


-- Get all the previous recipients:

tell application "System Events" to tell process "Mail"

click menu item "Previous Recipients" of menu 1 of menu bar item "Window" of menu bar 1

key code 51 -- ⌫ to remove the whole contents of the search field

keystroketab

delay 0.3

tell rows of table 1 of scroll area 1 of window "Previous Recipients" to ¬

set {theNames, theEmails} to {valueoftext field 1, valueoftext field 2}

keystroke "w" usingcommand down -- close the "Previous Recipients" window

endtell


settctocounttheNames

if tc > 0 then

settheNewEmailsto {}

settheNewFirstNamesto {}

settheNewLastNamesto {}

setotidtotext item delimiters

settext item delimitersto " "

repeatwithifrom 1 totc

setthisNametoitemioftheNames

if (thisName is not "") and ("@" is not in thisName) then

set t to text items of thisName

set end of theNewLastNames to last item of t

set end of theNewEmails to item i of theEmails

if (count t) > 1 then

set end of theNewFirstNames to (items 1 thru -2 of t) as string

else

set end of theNewFirstNames to ""

end if

end if

endrepeat

settext item delimiterstootid

settcto (counttheNewEmails)

if tc > 0 then

-- Add those previous recipients to the contacts, in the “Previous recipients” group:

tell application "Address Book"

launch

settheGrouptogrouptheGroupName

tell theGroup to set theAlreadyAddedEmails to (value of first email of peoplewhose its label is "eMail")

set k to 0

repeat with j from 1 to tc

set thisEmail to item j of theNewEmails

ifthisEmailis notintheAlreadyAddedEmailsthen

tell (make new person with properties ¬

{first name:item j of theNewFirstNames, last name:item j oftheNewLastNames})

make new email at end of emails with properties {label:"eMail",value:thisEmail}

add to theGroup

set k to k + 1

end tell

end if

end repeat

save

activate

set selected of theGroup to true

display dialog "Done (" & (k as text) & " contacts added)." buttons {"OK"} default button 1 with icon 1 giving up after 2

end tell

end if

endif

Mail Previous Recipients "port" to /folder/ in Contacts

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.