Edit a script

The script below is a modified version because the original version is more time consuming when it comes to testing the script.

set friendslist to {"a", "Mi", "am", "self"}
set chose_friends to (choose from list friendslist with prompt "Friend(s) to send email to:" with multiple selections allowed)
set thefriend to the result
if thefriend contains {"Mi"} then
	set theAddress to "Mi10@gmail.com"
	my sendemail(thefriend, theAddress)
end if
if thefriend contains {"am"} then
	set theAddress to "sf@gmail.com"
	my sendemail(thefriend, theAddress)
end if
if thefriend contains {"self"} then
        set theAddress to "bn@gmail.com"
	my sendemail(thefriend, theAddress)
end if
if thefriend contains {"a"} then
	set theAddress to "aue@gmail.com"
	my sendemail(thefriend, theAddress)
end if
on sendemail(thefriend, theAddress)
	set theName to thefriend
	set theName to theName as text
	display dialog theName & return & theAddress
end sendemail

Question 1:
I am going to add more friends to this list.
I want to avoid writing the “if” statements four times and improve this script so that
–it works faster
–more friends can be added easily without having to make many changes at different places in the script

Question 2:
When I select multiple friends in the prompt (dialog box), separate emails are sent. I would like to send one email using “Bcc”(which hides the other receipients from the receiver of the email). I am unable to modify the script to make use of “Bcc” .

Please help.

try this


property friendslist : {"a", "Mi", "am", "self"}
property emailList : {"aue@gmail.com", "Mi10@gmail.com", "sf@gmail.com", "bn@gmail.com"}
property myEmail : "john@doe.com"

set chose_friends to (choose from list friendslist with prompt "Friend(s) to send email to:" with multiple selections allowed)
if chose_friends is false then return
if (count chose_friends) > 1 then
	sendBCCmail(chose_friends)
else
	repeat with i from 1 to count friendslist
		if chose_friends contains {item i of friendslist} then
			sendemail(item i of friendslist, item i of emailList)
			exit repeat
		end if
	end repeat
end if

on sendemail(thefriend, theAddress)
	set theName to thefriend
	set theName to theName as text
	display dialog theName & return & theAddress
end sendemail

on sendBCCmail(addressList)
	tell application "Mail"
		set newMessage to make new outgoing message with properties {visible:true, subject:"newMail"}
		tell newMessage
			make new to recipient at end of to recipients with properties {address:myEmail}
			repeat with i from 1 to count emailList
				if {item i of friendslist} is in addressList then
					make new bcc recipient at end of bcc recipients with properties {address:item i of emailList}
				end if
			end repeat
		end tell
		activate
	end tell
end sendBCCmail

Thanks :slight_smile: