Create contacts from messages in chosen mailbox

Hi all,
taking a cue from various scripts, I managed to write a script to create contacts in Address Book from messages of the chosen mailbox in Mail. It works great, but every time I have to write the name of the mailbox being careful case-sensitive, and it is especially annoying with long names.
So I decided to modify it in order that I have only to choose the mailbox, but something is going wrong…

Here the first working script :

tell application "Mail"
          set dialogResult1 to display dialog "Inserire il nome della cartella che contiene i contatti da aggiungere :" default answer "" buttons {"Cancella", "OK"} default button 2 with icon 1
          if button returned of dialogResult1 = "OK" and text returned of dialogResult1 is not equal to "" then
                    if mailbox (text returned of dialogResult1) exists then
                              set mymailbox to mailbox (text returned of dialogResult1)
                              set msgs to every message in mymailbox
                              set nrmsg to 1
                              set totmsg to (count (messages in mymailbox))
                              set grpname to ""
                              set theGroup to ""
                              repeat with mymessage in msgs
                                        set theSenderName to extract name from sender of item nrmsg of msgs
                                        set theEmail to extract address from sender of item nrmsg of msgs
                                        tell application "Contacts"
                                                  activate
                                                  if theGroup = "" then
                                                            set dialogResult2 to display dialog "Inserire il nome del gruppo a cui aggiungere i contatti :" default answer "" buttons {"Cancella", "OK"} default button 2 with icon 1
                                                            if button returned of dialogResult2 = "OK" and text returned of dialogResult2 is not equal to "" then
                                                                      set theGroup to group (text returned of dialogResult2)
                                                                      set thePerson to make new person with properties {first name:theSenderName}
                                                                      make new email at end of emails of thePerson with properties {label:"Work", value:theEmail}
                                                                      add thePerson to theGroup
                                                                      save
                                                                      set grpname to theGroup
                                                                      set nrmsg to nrmsg + 1
                                                            else
                                                                      beep
                                                                      display dialog "Il gruppo specificato non esiste." buttons {"OK"} default button 1 with icon 2
                                                            end if
                                                  else
                                                            set thePerson to make new person with properties {first name:theSenderName}
                                                            make new email at end of emails of thePerson with properties {label:"Work", value:theEmail}
                                                            add thePerson to theGroup
                                                            save
                                                            set nrmsg to nrmsg + 1
                                                  end if
                                        end tell
                              end repeat
                    else
                              beep
                              display dialog "La cartella specificata non esiste." buttons {"OK"} default button 1 with icon 2
                    end if
          end if
end tell

and here the second from which I get nothing :

tell application "Mail"
          set theList to name of mailboxes
          set thePrompt to "Seleziona la mailbox da cui estrarre i contatti."
          set mymailbox to choose from list theList with prompt thePrompt --with multiple selections allowed
          if mymailbox is false then return
          set msgs to every message in mymailbox
          set nrmsg to 1
          set totmsg to (count (messages in mymailbox))
          set grpname to ""
          set theGroup to ""
          display dialog (mymailbox & " - " & totmsg as rich text) buttons {"OK"} default button 1
          repeat with mymessage in msgs
                    set theSenderName to extract name from sender of item nrmsg of msgs
                    set theEmail to extract address from sender of item nrmsg of msgs
                    tell application "Contacts"
                              activate
                              if theGroup = "" then
                                        set dialogResult2 to display dialog "Inserire il nome del gruppo a cui aggiungere i contatti :" default answer "" buttons {"Cancella", "OK"} default button 2 with icon 1
                                        if button returned of dialogResult2 = "OK" and text returned of dialogResult2 is not equal to "" then
                                                  set theGroup to group (text returned of dialogResult2)
                                                  set thePerson to make new person with properties {first name:theSenderName}
                                                  make new email at end of emails of thePerson with properties {label:"Work", value:theEmail}
                                                  add thePerson to theGroup
                                                  save
                                                  set grpname to theGroup
                                                  set nrmsg to nrmsg + 1
                                        else
                                                  beep
                                                  display dialog "Il gruppo specificato non esiste." buttons {"OK"} default button 1 with icon 2
                                        end if
                              else
                                        set thePerson to make new person with properties {first name:theSenderName}
                                        make new email at end of emails of thePerson with properties {label:"Work", value:theEmail}
                                        add thePerson to theGroup
                                        save
                                        set nrmsg to nrmsg + 1
                              end if
                    end tell
          end repeat
end tell

During execution of the second script no error occurs, simply it does not count the number of messages in the selected mailbox and therefore do not get the desired result.
Any help is really appreciate.
Thanks.

Hi,

two problems:

¢ choose from list returns always a list of items, so the list must be flattened.
¢ the return value is just a string but you need a mailbox object reference.
Insert one line


.
	if mymailbox is false then return
	set mymailbox to 1st mailbox whose name is (item 1 of my mailbox) -- insert this line
	set msgs to every message in mymailbox
.

Hi Stefan,
thanks for your interest.

I applied your suggestion but an error occurred : error "Mail got an error: Can’t get " number -1719. I solved it by changing the name of the variable :


	---
	if mymailbox is false then return
	set chosenmailbox to 1st mailbox whose name is (item 1 of mymailbox)
	set msgs to every message in chosenmailbox
	---

Now it works fine, thank you very much.

Trying to improve the script, I thought to insert the choice of the group instead of having to write his name (as per the choice of the mailbox). It works, but I would like to avoid creating duplicates if there are more emails containing the same data (ie same name and same address). I might add at the end the deletion of duplicates, but it could be a risk.
Can you give me some advice?

Here is the new working code :


	tell application "Mail"
	set theMBList to name of mailboxes
	set theMBPrompt to "Seleziona la mailbox da cui estrarre i contatti."
	set mymailbox to choose from list theMBList with prompt theMBPrompt --with multiple selections allowed
	if mymailbox is false then return
	set chosenmailbox to 1st mailbox whose name is (item 1 of mymailbox)
	set msgs to every message in chosenmailbox
	set nrmsg to 1
	set totmsg to (count (messages in chosenmailbox))
	set theGroup to ""
	repeat with mymessage in msgs
		set theSenderName to extract name from sender of item nrmsg of msgs
		set theEmail to extract address from sender of item nrmsg of msgs
		tell application "Contacts"
			activate
			if theGroup = "" then
				set theCntList to name of groups
				set theCntPrompt to "Seleziona il gruppo a cui aggiungere i contatti."
				set mygroup to choose from list theCntList with prompt theCntPrompt --with multiple selections allowed
				if mygroup is false then return
				set chosengroup to (1st group whose name is (item 1 of mygroup))
				set thePerson to make new person with properties {first name:theSenderName}
				make new email at end of emails of thePerson with properties {label:"Work", value:theEmail}
				add thePerson to chosengroup
				save
				set theGroup to mygroup
				set nrmsg to nrmsg + 1
			else
				set thePerson to make new person with properties {first name:theSenderName}
				make new email at end of emails of thePerson with properties {label:"Work", value:theEmail}
				add thePerson to chosengroup
				save
				set theGroup to mygroup
				set nrmsg to nrmsg + 1
			end if
		end tell
	end repeat
end tell

Thanks for your help!!!

this script

extracts all names/addresses from the messages
creates a record of each name/address pair and adds the record to a list if the list does not contain the record.
asks for the address book group
creates the people in a second repeat loop


tell application "Mail"
	set theMBList to name of mailboxes
	set theMBPrompt to "Seleziona la mailbox da cui estrarre i contatti."
	set mymailbox to choose from list theMBList with prompt theMBPrompt --with multiple selections allowed
	if mymailbox is false then return
	set chosenmailbox to mailbox (item 1 of mymailbox)
	set msgs to every message in chosenmailbox
	set senderList to {}
	repeat with mymessage in msgs
		set senderRecord to {senderName:extract name from sender of mymessage, senderAddress:extract address from sender of mymessage}
		if senderList does not contain {senderRecord} then set end of senderList to senderRecord
	end repeat
end tell

tell application "Contacts"
	activate
	set theCntList to name of groups
	set theCntPrompt to "Seleziona il gruppo a cui aggiungere i contatti."
	set mygroup to choose from list theCntList with prompt theCntPrompt --with multiple selections allowed
	if mygroup is false then return
	set theGroup to group (item 1 of mygroup)
	repeat with aSender in senderList
		set thePerson to make new person with properties {first name:senderName of aSender}
		make new email at end of emails of thePerson with properties {label:"Work", value:senderAddress of aSender}
		add thePerson to theGroup
	end repeat
	save
end tell


Stefan, in addition to solving my problem you have also optimized the code (my version was much more rough).
I have to learn still so much to be able to do it alone.
Thanks for your help.

Di niente

(you’re welcome)