This was my fall back plan but, have you guessed I might need some more help?
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.
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,
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
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.
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.
after testing a few things I also noticed,
that writing in an open draft message has no effect
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
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:
Manually start a new message, adding an e-mail in the To field.
Run the script⦠which then saves the message to the drafts mailbox and closes the window.
The script now does itās thing and changes the sender account.
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.