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 27, 2012 5:49 PM in response to hotwheels22

I have tested the script. It seems to work flawlessly, provided the name of the group is the same in the Address Book as in the script (although uppercase letters are not distinguished from lowercase letters). If the names were different, the script would return an AppleScript error (Contacts got an error: Can’t get group "previous recipients") and shouldn't add any new card into the “All Contacts” section of the Address Book.

Aug 27, 2012 7:33 PM in response to Pierre L.

hi pierre.


can you repost the script so i know where i am copying it from? i have edited the script and it is both dumping the emails into the "All Contacts" section instead of putting it in a folder but also it seems to be creating duplicates if i run it again which is a bit of a problem.


i would like to test it again but i have mixed things up over here i am afraid.


also, i am not sure if anyone more sophisticated than i am is following this, but it would be very helpful to know how this is getting handled with respect to "DUPLICATES". for instance, i really cannot run this script if it is replacing existing contacts (i.e. first name BOB and second name SMITH and a PHONE NUMBER) with a new contact that is first name BOB and second name SMITH with an email address. I /think/ this will not happen with this script if it will put everything in one folder but if it puts them in the ALL CONTACTS section i think it will overwrite the existing contacts which will kill my database.


anyway, can you repost the script so i can try it again. it looked great when i did it the first time before editing...


jon

Aug 27, 2012 9:37 PM in response to Pierre L.

hi pierre.


something is not working.


i tried this initially and it sort of worked but it added contacts to "Previous recipients_MP" instead of "Previous recipients_MBP".


i am using the script below which was originally posted first and it is definitely adding 18 contacts to the All Contacts section and not in the "Previous Recipients" folder.


so then i tested by just creating a "test group" folder and then doing as you indicate here and it still does not add the contact to the newly created folder. i can test it on the mac pro but it would be great to get it straight on the macbook pro.


is it possible i have to compile it or i have to open or close contacts or address book before running...?


set theGroup to group "Previous recipients"

with this one:

set theGroup to group "Test Group"


***


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

keystroke "a" usingcommand down-- select the whole contents of the search field

key code 51 -- remove it

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

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 group "Previous recipients"

repeat with k from 1 to (count theNewEmails)

set thisFirstName to itemk of theNewFirstNames

set thisLastName to itemk of theNewLastNames

set thisEmail to itemk of theNewEmails

set thisPerson to makenewpersonwith properties ¬

{first name:thisFirstName, last name:thisLastName}

makenewemailat end of emails of thisPersonwith properties ¬

{label:"eMail", value:thisEmail}

addthisPersontotheGroup

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

end tell

Aug 28, 2012 3:27 AM in response to hotwheels22

Jon,


I have tested the script (the one in your very last post) again on my MacBook Pro, just changing "Previous recipients" for "Test Group". And again the script worked flawlessly (at least from my point of view).


A few remarks:


It doesn't seem to matter whether Contacts.app and Mail.app are open or not before running the script.


It doesn't seem to matter whether you compile the script or not before running it. Clicking the Run button just after pasting the script into the AppleScript Editor window is exactly the same as clicking Compile first, then Run.


By the way, since the beginning of this thread, I don't understand what you mean by “folder”. For me, there are just groups in the Contacts.app window, all listed below All Contacts. And I don't see the word “folder” anywhere in Mail's menus.


Of course, each time the script is run, new cards are added to All Contacts, it cannot be otherwise. But these new cards are also added to the Test Group; they will also be deleted from All Contacts when deleted (not just removed) from the Test Group.


And yes, in it' present state, if you run the script three times in line, you'll get three instances of each new card in your Test Group, without overwriting anything though. However that could probably be changed using an if clause, or otherwise.


Message was edited by: Pierre L.

Aug 28, 2012 4:26 AM in response to Pierre L.

Here's a new (somewhat slower) version of the script that shouldn't create any duplicate in the “Test Group” group:


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 group "Test Group"

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

end tell

Hope it can help.

Aug 28, 2012 7:41 AM in response to Pierre L.

ok. got to work on this some more over here.


i got it to work in 10.7.4 but have to figure out what is going on in 10.8 now.


i wonder if it is necessary to create a new folder instead of the "test group" when doing this a second time on another computer?


or perhaps there is an interactive way to /name/ the new group while running the script?


also, it appears to be creating a lower case "test group" itself so i don't have to create the new folder manually before running it...


THANKS

Aug 28, 2012 8:15 AM in response to hotwheels22

i wonder if it is necessary to create a new folder instead of the "test group"


Have you read each of my last three posts? I'm asking because you're talking again of a "folder", and I don't understand what you mean by that. Do you just mean “group” when using the word “folder” or do you actually mean some real folder somewhere in the folder hierarchy?

Aug 28, 2012 8:16 AM in response to Pierre L.

hmm.


worked in 10.7.4 as described above (the new "test group") was created and previous recipients from that machine were added.


now when i open the rcp file on the laptop machine i get a done "zero contacts were added" even though there are obviously different previous recipients in the Mac Mail database on this machine. so for some reason it is not seeing these contacts on this machine when i run the script.


hmmm. i even copied the new script again from the new post and created a new script and ran that one on the laptop and it doesn't add new contacts.


maybe i need to manually change the name of the group from "test group" to test group 2 in the script...?


THANKS

Aug 28, 2012 10:27 AM in response to hotwheels22

i should manually create a folder in Contacts/Address Book with the same name as the one described in the script is that right?


That's right. At least, that's what I do on my MacBook Pro.


and then if i run it on two machines it will put all the previous recipients from each machine into this single group is that right?


Is that right? That's all the question. As soon as you are talking about two machines, surely you are talking about iCloud and some sort of synchronisation. Unfortunately, I don't use iCloud and know practically nothing about it. But the more I think about your issues, the more it seems to me that there lies the source of all your problems, in iCloud. If that's the case, i can't be of no help.

Aug 28, 2012 3:24 PM in response to Pierre L.

hi pierre.


can i please just ask you for a version that puts the information in a group named "previous recipients" and one that puts the information in a group named "previous recipients_MBP". i am wondering if my /manual/ renaming of the destination group in the script has caused a problem somewhere.


there is no iCloud or any other synchronization of Previous Recipients in Mac Mail. this is the whole point of doing these scripts. this database /only/ resides on ONE computer and there is no sync and no export at all except a really lame ability to add to Contacts. this add to Contacts is so lame it is not even worth doing because it will not let me maintain it in any way that makes sense.


if i can just get these two scripts to work on both machines it would be great. maybe deliberately having TWO groups will work if i try this again.


then i can delete the "test group"...


if i do this manually do i just need to change ONE LINE of code to do this? or every entry that used to say "test group" so that it now says the new group name...?

Aug 28, 2012 4:51 PM in response to hotwheels22

can i please just ask you for a version that puts the information in a group named "previous recipients" and one that puts the information in a group named "previous recipients_MBP".



The following very slightly modified version of the script puts the information in a group named "previous recipients". To get a version that puts the information in a group named "previous recipients_MBP", just replace "previous recipients" with "previous recipients_MBP" in the very first line of the script. Absolutely no way to go wrong here!



set theGroupName to "previous recipients" -- 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

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.