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.