Set Mail account by Address Book group?

Hi all,

Had some excellent help yesterday with a script to automatically change a sending account in Mail when making a selection in a list:
http://bbs.applescript.net/viewtopic.php?id=19664

This was my fall back plan but, have you guessed I might need some more help? :stuck_out_tongue:

Well, what I’d REALLY like to be able to do is run a script that checks the To: address in an open draft message against all the contacts in Address Book. Then, if it finds it, it looks to see which group the address is in and tells Mail to change the sending account accordingly. This is because I have 14 aliases for my domain name (e.g. friends@domain.com, family@domain.com), with groups of people being sent e-mail from those different aliases. A script would stop me making a mistake and sending from the wrong account.

I’ve found a script that would do what I need (having searched for three days), but it’s for OS 9, and Entourage:
http://scriptbuilders.net/files/setaccountbycategory1.0.html

Is this something that’s possible with Mail and Address Book? There’s no way I’ll be able to work this out myself so any help would be greatly appreciated,

Justy

Hi Justy,

it’s also possible with Mail and Address Book
with something like this:


set GroupList to {"Group1", "Group2"}
set AccountList to {"Account1", "Account2"}

tell application "Mail" to set theRecipient to name of to recipient 1 of message 1 of drafts mailbox

tell application "Address Book"
	repeat with i in groups
		if theRecipient is in (get name of people of i) then
			set theGroup to name of i
			exit repeat
		end if
	end repeat
end tell
repeat with j from 1 to count GroupList
	if theGroup is item j of GroupList then
		set theAccount to item j of AccountList
		exit repeat
	end if
end repeat

tell application "Mail" to set sender of message 1 of drafts mailbox to theAccount

Hi again Stefan,

Thanks for that. However, it doesn’t seem to do anything. This is the info I’ve put in the top section (the names in the GroupList are as they’re shown in Address Book):

set GroupList to {"Friends", "Family"}
set AccountList to {"Justy <friends@domain.com>", "Justy <family@domain.com>"}

If in the Mail message I put an address that’s not in one of those two Address Book groups then it errors with “The variable theAccount is not defined”. Can we get an error dialog pop up for that to say e-mail address not in any group? And if I put an address that IS there, nothing changes in the Mail message.

Justy

Almost there,

Discovered that it works to a certain extent if you close the message window, run the script, then open the draft message again, but it’s a bit of a process.

Any further thoughts?

J

Hi Justy,

after testing a few things I also noticed,
that writing in an open draft message has no effect :confused:

A workaround (I know it’s not very elegant) is to copy all data (cc, bcc, and attachments are not (yet) included)
close the window, delete the message and make a new one with the new account and the saved data

set GroupList to {"Friends", "Family"}
set AccountList to {"Justy <friends@domain.com>", "Justy <family@domain.com>"}

tell application "Mail"
	tell message 1 of drafts mailbox
		set {theContent, theSubject, theMessage} to {content, subject, it}
		tell to recipient 1 to set {theRecipientName, theRecipientAddress} to {name, address}
	end tell
	close (get 1st window whose name is theSubject)
	delete theMessage
end tell

tell application "Address Book"
	set theGroup to missing value
	repeat with i in groups
		if theRecipientName is in (get name of people of i) then
			set theGroup to name of i
			exit repeat
		end if
	end repeat
end tell
if theGroup is missing value then
	display dialog "The recipient isn't in any group of Address Book" buttons {"OK"} default button 1
	return
end if
repeat with j from 1 to count GroupList
	if theGroup is item j of GroupList then
		set theAccount to item j of AccountList
		exit repeat
	end if
end repeat

tell application "Mail"
	set newMessage to make new outgoing message with properties {visible:true, subject:theSubject, content:theContent}
	tell newMessage
		set sender to theAccount
		make new to recipient at end of to recipients with properties {name:theRecipientName, address:theRecipientAddress}
	end tell
	activate

end tell

Hi Stefan,

Thanks for the continued help with this.

Tried the new script and it’s certainly better. However, I get duplicates of the same message and the subject can come up blank.

I had a thought this afternoon that we could:

  1. Manually start a new message, adding an e-mail in the To field.
  2. Run the script… which then saves the message to the drafts mailbox and closes the window.
  3. The script now does it’s thing and changes the sender account.
  4. The script now opens the message again and we’re ready to type in the content and send.

As a point of interest though, I’m using SignatureProfiler, which automatically changes the signature based on the sending account. This works when changing the account manually (choosing from the drop-down menu), but because the script we have here won’t change the account in the open window, it’s not allowing SigPro to make a change.

Might have to change to Entourage, afterall :wink:

Justin