As you may see below, I made some changes.
tell application "Mail"
set emailAddressList to {}
repeat with thisAccount in accounts
set emailAddresses to email addresses of thisAccount
repeat with anAddress in emailAddresses
copy (anAddress as string) to end of emailAddressList
end repeat
end repeat
set theMessages to selection
repeat with a from 1 to count of theMessages
set thisMessage to item a of theMessages
-- set thisMessageID to id of thisMessage # UNUSED
set thisSender to sender of thisMessage
tell me to set cleanedSenderAddress to do shell script "echo " & quoted form of thisSender & " | cut -d '<' -f2- | cut -d '>' -f1"
if cleanedSenderAddress is not in emailAddressList then
set senderReceiverName to sender of thisMessage
tell me to set senderReceiverName to do shell script "echo " & quoted form of senderReceiverName & " | cut -d '<' -f1 | sed 's/ *$//'"
display dialog "sent by : " & senderReceiverName
else -- if cleanedSenderAddress is in emailAddressList then
set senderReceiverName to name of item 1 of (every to recipient of thisMessage)
if senderReceiverName is missing value then set senderReceiverName to address of item 1 of (every to recipient of thisMessage)
display dialog cleanedSenderAddress & " sent this message to " & senderReceiverName
end if
end repeat
end tell
(1) your instruction : copy (email addresses of account i as string) to end of emailAddressList
didn’t added what was needed when an account has several addresses.
It inserted “@me.com@icloud.com**********@mac.com” when we wanted it to insert “@me.com", "@icloud.com”, “**********@mac.com”
(2) In your main if then else instruction, there was no need for:
[format]else if cleanedSenderAddress is in emailAddressList then[/format]
a simple
[format]else[/format]
is sufficient
(3) when executed, your instruction : set cleanedSenderAddress to do shell script “echo " & quoted form of thisSender & " | cut -d ‘<’ -f2- | cut -d ‘>’ -f1”
generated :
[format] do shell script “echo ‘**** ****** **********@mac.com’ | cut -d ‘<’ -f2- | cut -d ‘>’ -f1”
tell current application
do shell script “echo ‘**** ****** **********@mac.com’ | cut -d ‘<’ -f2- | cut -d ‘>’ -f1”
end tell[/format]
Inserting tell me to at beginning drops the first failing call.
[format]tell current application
do shell script “echo ‘**** ****** **********@mac.com’ | cut -d ‘<’ -f2- | cut -d ‘>’ -f1”
end tell[/format]
(4) In the else part of the code, when it’s availale, name of… is a string
When name of… is missing value I extract the address of the target.
In both case we get a string so there is no need to trigger do shell script.
An alternate structure may be simpler and easier to read:
tell application "Mail"
set emailAddressList to {}
repeat with thisAccount in accounts
set emailAddresses to email addresses of thisAccount
repeat with anAddress in emailAddresses
copy (anAddress as string) to end of emailAddressList
end repeat
end repeat
set theMessages to selection
repeat with a from 1 to count of theMessages
set thisMessage to item a of theMessages
--set thisMessageID to id of thisMessage # UNUSED
set cleanedSenderAddress to my getAddress(sender of thisMessage)
if cleanedSenderAddress is not in emailAddressList then
set senderReceiverName to my getAddress(sender of thisMessage)
display dialog "sent by : " & senderReceiverName
else if cleanedSenderAddress is in emailAddressList then
set senderReceiverName to name of item 1 of (every to recipient of thisMessage)
if senderReceiverName is missing value then set senderReceiverName to address of item 1 of (every to recipient of thisMessage)
display dialog cleanedSenderAddress & " sent this message to " & senderReceiverName
end if
end repeat
end tell
on getAddress(someOne)
return do shell script "echo " & quoted form of someOne & " | cut -d '<' -f2- | cut -d '>' -f1"
end getAddress
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 27 aout 2019 12:40:13