Set default sender in Mail.app

Hi

I want to set the default sender address for new messages. (The sender address that is preselected by Mail.app when “New” is clicked)
How to do this?

I see two possible approaches:

  • There is a read-only property “primary email” in Mail
  • There is a “NewMessageFromAddress” key in com.apple.mail.plist

Since I am totally new to AppleScript I need help.

Why are you trying to achieve this with AppleScript? There is a preference in Mail that allows you to choose the default address you are to use when composing new mail.

Well, how about this piece of code :wink:


on run
	-- getting all available sender accounts
	set senderaccounts to my getsenderaccounts()
	-- choosing the last one for demo purposes
	set senderaccount to last item of senderaccounts
	-- creating a new message with the chosen sender account
	tell application "Mail"
		set curmsg to make new outgoing message with properties {visible:true}
		tell curmsg
			set sender to senderaccount
		end tell
	end tell
end run

-- I'm returning all available sender accounts
on getsenderaccounts()
	set senderaccounts to {}
	tell application "Mail"
		set allaccounts to every account
		repeat with useraccount in allaccounts
			set useraddresses to email addresses of useraccount
			if (useraddresses is not equal to missing value) then
				repeat with useraddress in useraddresses
					set senderaccounts to senderaccounts & {(full name of useraccount & " <" & useraddress & ">") as Unicode text}
				end repeat
			end if
		end repeat
	end tell
	return senderaccounts
end getsenderaccounts

Hope this helps.

I want to set the default sender address based on my current location (Work, Home). For location detection I use MarcoPolo ( http://www.symonds.id.au/marcopolo/ ).

Thank you for this! I’m definitely will use the getsenderaccounts() method.

Probably I have to set the “NewMessageFromAddress” key in com.apple.mail.plist. Is there a tutorial for plist editing?

This will do the job:



on run
	-- getting all available sender accounts
	set senderaccounts to my getsenderaccounts()
	-- choosing the last one for demo purposes
	set senderaccount to last item of senderaccounts
	-- writing the info to the plist
	set command to "defaults write com.apple.mail NewMessageFromAddress \"'" & senderaccount & "'\""
	do shell script command
end run

-- I'm returning all available sender accounts
on getsenderaccounts()
	set senderaccounts to {}
	tell application "Mail"
		set allaccounts to every account
		repeat with useraccount in allaccounts
			set useraddresses to email addresses of useraccount
			if (useraddresses is not equal to missing value) then
				repeat with useraddress in useraddresses
					set senderaccounts to senderaccounts & {(full name of useraccount & " <" & useraddress & ">") as Unicode text}
				end repeat
			end if
		end repeat
	end tell
	return senderaccounts
end getsenderaccounts

Wow, thank you so much.