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

[亡魂溺海] 提交于 2019-12-18 12:34:53

问题


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?


回答1:


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.




回答2:


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 <dev@software-concepts.org>"

(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 <dev@software-concepts.org>")



回答3:


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:"hello@world.com", name:"The World"}
    set the sender to "Jerry Krinock <jerry@sheepsystems.com>"
    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.




回答4:


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




回答5:


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 <me@my.com>"}

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




回答6:


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. :)




回答7:


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.



来源:https://stackoverflow.com/questions/6638711/how-to-set-the-sender-of-the-current-mail-app-outgoing-message-via-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!