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

    arg. pierre. i ran this on my macbook pro and had a edit that was supposed to add these contacts to a folder called "previous recipients_MBP" but for some reason they were added to the "previous recipients_MP" folder which was supposed to have the contacts from the Mac Pro added to it.

     

    now i am trying to simplify and dump them both into a "previous recipients" folder but at first they seem to have been added to the "All Contacts" section and did not go into the "previous recipients" folder, so then i changed the script from "Previous recipients" to "previous recipients" with a lower case and now i am not seeing it work or work well.

     

    is it possible for you to look at the script below and tell me what i have done wrong? i think it is not a problem to have the folder set up in Contacts.app before running the script but for some reason i must have edited the code incorrectly or i am not creating the folder correctly??

     

    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" using command down -- select the whole contents of the search field

      key code 51 -- remove it

      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

    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 item k of theNewFirstNames

                        set thisLastName to item k of theNewLastNames

                        set thisEmail to item k of theNewEmails

                        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

              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

  • Pierre L. Level 4 Level 4 (3,950 points)

    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.

  • hotwheels 22 Level 1 Level 1 (0 points)

    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

  • hotwheels 22 Level 1 Level 1 (0 points)

    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" using command down -- select the whole contents of the search field

        key code 51 -- remove it

        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

    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 item k of theNewFirstNames

            set thisLastName to item k of theNewLastNames

            set thisEmail to item k of theNewEmails

            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

        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

  • Pierre L. Level 4 Level 4 (3,950 points)

    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.

  • Pierre L. Level 4 Level 4 (3,950 points)

    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

        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 "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 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

    end tell


    Hope it can help.

  • Pierre L. Level 4 Level 4 (3,950 points)

    Jon, you're under Mac OS X 10.6.8 while I'm under OS X 10.8. Maybe that could explain the discrepancies between your results and mine.

    Better late than never…

  • hotwheels 22 Level 1 Level 1 (0 points)

    hi pierre.

     

    i have to update the description.

     

    i am in fact on 10.8 on the laptop (which i am testing on) and i am on 10.7.4 on the desktop (the highest OS the machine will accept).

     

    if i can get it going on the laptop (10.8) i will test on desktop (10.7.4).

     

    thanks

  • hotwheels 22 Level 1 Level 1 (0 points)

    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

  • Pierre L. Level 4 Level 4 (3,950 points)

    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?

  • hotwheels 22 Level 1 Level 1 (0 points)

    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

  • hotwheels 22 Level 1 Level 1 (0 points)

    sorry...i mean "group".

  • hotwheels 22 Level 1 Level 1 (0 points)

    hi pierre.

     

    i can try this again.

     

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

     

    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?

     

    THANKS

     

    jon

  • Pierre L. Level 4 Level 4 (3,950 points)

    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.

  • hotwheels 22 Level 1 Level 1 (0 points)

    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...?