How to set the sender of the current Mail.app outgoing message via AppleScript?
Asked Answered
D

7

8

I'd like to write an AppleScript to set the sender of the current outgoing message in Apple's Mail.app.

I've tried this:

tell application "Mail" to set sender of front outgoing message to "<my email address>"

but I get the error message error "Mail got an error: Can’t set sender of item to any." number -10006 from sender of item to any which doesn't make sense to me.

When I try to interrogate the front outgoing message as follows:

tell application "Mail" to get properties of front outgoing message

I get {class:item} in return, instead of an "outgoing message" object like I'd expect.

Any ideas?

Diva answered 10/7, 2011 at 2:6 Comment(0)
E
5

Unfortunately, you cannot get or set the properties of the outgoing message object of Mail with Applescript.

Instead, you can accomplish this with GUI scripting, a workaround that directly manipulates window elements.

This code should work for you:

tell application "System Events"
  tell process "Mail"
    click pop up button 1 of window 1
    click menu item 6 of menu 1 of pop up button 1 of window 1
  end tell
end tell

Change the menu item 6 on the fourth line to whichever number in the list your desired sender is (e.g., if the sender you want to change to with this script is the fourth one listed, change menu item 6 to menu item 4).


Update: If you're curious, since this answer is over two years old: as of 2014-04-26, getting/setting the properties of outgoing messages is still impossible and this workaround still works in OS X 10.9 Mavericks.

Ethelind answered 7/10, 2011 at 10:41 Comment(1)
I'll add that if the email address you're trying to set isn't in that list, you can add it to the list easily. In Mail preferences, under Accounts, under the 'Account Information' panel, add the email address(es) you want to be able to send from with that account, separated by commas.Ethelind
S
4

I think it's a little more complicated. My own experiments agree with the conclusion of Alan Kimelman in this thread, that AppleScript works as expected for outgoing messages created from scratch by the script. For example, the following code works:

tell application "Mail"
set newMessage to (a reference to (make new outgoing message))
tell newMessage
    make new to recipient at beginning of to recipients ¬
        with properties {address:"[email protected]", name:"The World"}
    set the sender to "Jerry Krinock <[email protected]>"
    set the subject to "A Test"
    set the content to "This is only a test."
    send
end tell
end tell

However, if the message is created by other means (for example, I create HTML messages by telling Safari to Share via Email), then Matthew McVickar is correct that AppleScript is broken. You can't set or get any properties, and also it refuses the send command.

Slavocracy answered 5/6, 2014 at 0:23 Comment(1)
You have to make sure the sender name is exactly the same as the one in your dropdown menu to make it work.Greaseball
K
4

Actually you can set the sender of an outgoing message as described here: Incorrect sender when sending Email via Applescript

Kasi answered 13/12, 2014 at 11:52 Comment(0)
C
3

It is more than a little frustrating that you can't set the sender of an outgoing message.

Here's a more general version of Matthew's script that I wrote many years ago and have used frequently. The argument is a string that contains exactly what you see in the "From:" pop up, e.g.

"Mitchell L Model <[email protected]>"

(It is very tricky to get the details of something like this right if you don't do a lot of GUI scripting.)

to setFrom(address)
    tell application "System Events"
        activate application "Mail"
        tell pop up button "From:" of window 1 of application process "Mail"
            click
            tell menu item address of menu 1 to click
        end tell
    end tell
end setFrom

The reason I use a parameterized handler is that I use keystroke macro facilities to bind a keystroke combination for each of my email addresses. Each executes an AppleScript that loads the file containing the above handler and invokes it with a specific email address. For example:

property util : load script alias (((path to home folder) as text) & "Scripts:Utilities.scpt")

util's 's setFrom("Mitchell L Model <[email protected]>")
Clepsydra answered 18/7, 2013 at 22:2 Comment(1)
Great, thanks. @Matthew's answer didn't work when replying to messages with the "High Priority" flag set, since the pop up button number changed. But using the phrase pop up button "From:" works regardless.Diva
W
0

Try this instead.

tell application "Mail" to make new outgoing message with properties {sender:"<your_email_address>", visible:true}

UPDATE: I would look in Mail's scripting dictionary and see what you can and cannot do. The information provided there might help. :)

Wharve answered 29/7, 2011 at 19:56 Comment(1)
Thanks, but I'd like to change the sender of the current message, not create a new one.Diva
E
0

An alternative to the GUI scripting example I provided would be an Automator script that utilizes the 'Watch Me Do' command. I don't understand exactly how it works underneath the hood, but it ostensibly records mouse movement and GUI interaction and then reenacts what you recorded it when it runs. Unfortunately, I experienced significant lag when running the script. After asking it to run, it would sometimes execute after 5 or more seconds, which is clearly unusable.

The GUI scripting method is almost instantaneous and cleaner-looking (no mouse movement) to boot. I recommend it.

Ethelind answered 7/10, 2011 at 11:6 Comment(0)
B
0

It's trivial to use AppleScript to create a new outgoing message with any outgoing address that you like (well, at least, the ones configured in your Mail.app preferences).

The discussions here hinge around trying (incorrectly) to change it after the message is created. Instead, specify it when you create the message:

set new_m to make new outgoing message with properties {sender:"My Name <[email protected]>"}

The text in quotes needs to match both the real name and the email address (in chevrons) of any configured account. If there's no match, Mail.app will use the default address

Brutality answered 23/3, 2015 at 20:25 Comment(1)
This does not address the original question: the message has been created by other means, not by AppleScript.Inedited

© 2022 - 2024 — McMap. All rights reserved.