Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 224
smokris Avatar asked Jul 10 '11 02:07

smokris


2 Answers

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.

like image 199
Matthew McVickar Avatar answered Oct 23 '22 12:10

Matthew McVickar


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.

like image 4
Jerry Krinock Avatar answered Oct 23 '22 12:10

Jerry Krinock