Previous 1 2 3 Next 37 Replies Latest reply: Aug 29, 2012 1:47 PM by hotwheels 22 Go to original post
  • Pierre L. Level 5 Level 5 (4,040 points)

    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

        keystroke tab

        keystroke "a" using command 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" using command 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 theGroupName

        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 item j of theNewEmails

            if not (thisEmail is in theAlreadyAddedEmails) then

                set thisFirstName to item j of theNewFirstNames

                set thisLastName to item j of theNewLastNames

                set thisPerson to make new person with properties ¬

                    {first name:thisFirstName, last name:thisLastName}

                make new email at end of emails of thisPerson with properties ¬

                    {label:"eMail", value:thisEmail}

                add thisPerson to theGroup

                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


  • hotwheels 22 Level 1 Level 1 (0 points)

    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

      keystroke tab

      keystroke "a" using command 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" using command 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 theGroupName

              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 item j of theNewEmails

                        if not (thisEmail is in theAlreadyAddedEmails) then

                                  set thisFirstName to item j of theNewFirstNames

                                  set thisLastName to item j of theNewLastNames

                                  set thisPerson to make new person with properties ¬

                                            {first name:thisFirstName, last name:thisLastName}

      make new email at end of emails of thisPerson with properties ¬

                                            {label:"eMail", value:thisEmail}

      add thisPerson to theGroup

                                  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

  • hotwheels 22 Level 1 Level 1 (0 points)

    yeah, i changed the first line to read "previous recipients" (using the same group as the contacts added on the Mac Pro) and i get:

     

    "Done zero (0) contacts added" even though there are contacts in the Previous Recipients in Mail on the macbook pro...

  • hotwheels 22 Level 1 Level 1 (0 points)

    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

  • Pierre L. Level 5 Level 5 (4,040 points)

    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.

  • hotwheels 22 Level 1 Level 1 (0 points)

    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

  • hotwheels 22 Level 1 Level 1 (0 points)

    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


     


  • hotwheels 22 Level 1 Level 1 (0 points)

    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/message/19418535?ac_cid=op123456#19418535

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

     

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

    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

         keystroke tab

         delay 0.3

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

              set {theNames, theEmails} to {value of text field 1, value of text field 2}

       

         keystroke "w" using command down -- close the "Previous Recipients" window

    end tell

     

    set tc to count theNames

    if tc > 0 then

         set theNewEmails to {}

         set theNewFirstNames to {}

         set theNewLastNames to {}

         set otid to text item delimiters

         set text item delimiters to " "

         repeat with i from 1 to tc

              set thisName to item i of theNames

              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

         end repeat

         set text item delimiters to otid

       

         set tc to (count theNewEmails)

         if tc > 0 then

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

              tell application "Address Book"

                   launch

                   set theGroup to group theGroupName

                   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

                        if thisEmail is not in theAlreadyAddedEmails then

                             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

    end if

Previous 1 2 3 Next