My First Ever Applescript...

:frowning:

Hello all

I’ve just written my first ever Applescript, but seem to be tying myself up in all sort of knots.

My script works fine, but I’ve got three email accounts on my Mac, and I’m struggling to get Applescript to recognise the correct one.

The email account that I want Applescript to use is the last one in the list (number three). I’ve tried:

tell account 3
and
tell account “my-email-account-name”

but it always uses the default account.

Anyone got any suggestions?

Regards

Graham

tell application "Mail"
	
	tell account "my-email-account-name"
		
		tell application "Mail"
			account "MPH Articles"
			set newMessage to make new outgoing message with properties {subject:"E-mail Subject", content:"Hello"}
			
			tell newMessage
				make new to recipient at end of to recipients with properties {address:"the-recipient-email-address"}
				set visible to true
				(*send*)
			end tell
			
		end tell
		
	end tell
end tell

Hi Graham,

this example from a script by Apple is quite extensive,
but it shows how to select an account to send a mail with

tell application "Mail"
	set listOfSenders to {}
	set everyAccount to every account
	repeat with eachAccount in everyAccount
		set everyEmailAddress to email addresses of eachAccount
		if (everyEmailAddress is not equal to missing value) then
			repeat with eachEmailAddress in everyEmailAddress
				set listOfSenders to listOfSenders & {(full name of eachAccount & " <" & eachEmailAddress & ">") as string}
			end repeat
		end if
	end repeat
end tell

set theResult to choose from list listOfSenders with prompt ¬
	"Which account would you like to send this message from?" without multiple selections allowed
if theResult is not equal to false then
	set theSender to item 1 of theResult
end if

tell application "Mail"
	set newMessage to make new outgoing message with properties {visible:true, subject:"E-mail Subject", content:"Hello"}
	tell newMessage
		set sender to theSender
		make new to recipient at end of to recipients with properties {address:"the-recipient-email-address"}
		(*send*)
	end tell
end tell