简体   繁体   English

如何通过AppleScript设置当前Mail.app外发邮件的发件人?

[英]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.我想写一个 AppleScript 在 Apple 的 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但我收到错误消息error "Mail got an error: Can't set sender of item to any." number -10006 from sender of item to any 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. error "Mail got an error: Can't set sender of item to any." number -10006 from sender of item to any

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.我得到{class:item}作为回报,而不是我期望的“传出消息” object 。

Any ideas?有任何想法吗?

Unfortunately, you cannot get or set the properties of the outgoing message object of Mail with Applescript.不幸的是,您无法获取或设置带有 Applescript 的 Mail 的outgoing message object 的属性。

Instead, you can accomplish this with GUI scripting, a workaround that directly manipulates window elements.相反,您可以使用 GUI 脚本来完成此操作,这是一种直接操作 window 元素的解决方法。

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 (eg, if the sender you want to change to with this script is the fourth one listed, change menu item 6 to menu item 4 ).将第四行的menu item 6更改为列表中您想要的发件人的任何数字(例如,如果您要使用此脚本更改的发件人是列出的第四个,则将menu item 6更改为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 message s is still impossible and this workaround still works in OS X 10.9 Mavericks.更新:如果你很好奇,因为这个答案已经超过两年了:截至 2014-04-26,获取/设置outgoing message的属性仍然是不可能的,并且这种解决方法仍然适用于 OS X 10.9 Mavericks。

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.这是我多年前编写并经常使用的 Matthew 脚本的更通用版本。 The argument is a string that contains exactly what you see in the "From:" pop up, eg参数是一个字符串,其中包含您在“From:”弹出窗口中看到的内容,例如

"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.) (如果您不编写大量 GUI 脚本,那么要正确获取此类内容的详细信息是非常棘手的。)

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.我使用参数化处理程序的原因是我使用击键宏工具为我的每个 email 地址绑定击键组合。 Each executes an AppleScript that loads the file containing the above handler and invokes it with a specific email address.每个执行一个 AppleScript 加载包含上述处理程序的文件并使用特定的 email 地址调用它。 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>")

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 .我自己的实验同意 Alan Kimelman 在 此线程中的结论,即 AppleScript对于脚本从头开始创建的传出消息按预期工作 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.但是,如果消息是通过其他方式创建的(例如,我通过告诉 Safari 通过电子邮件共享来创建 HTML 消息),那么 Matthew McVickar 是正确的 AppleScript 已损坏。 You can't set or get any properties, and also it refuses the send command.您无法设置或获取任何属性,并且它也拒绝发送命令。

Actually you can set the sender of an outgoing message as described here: Incorrect sender when sending Email via Applescript实际上,您可以按此处所述设置传出消息的发件人: Incorrect sender when sent Email via Applescript

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).使用 AppleScript 使用您喜欢的任何外发地址创建新的外发邮件(至少,在您的 Mail.app 首选项中配置的地址)是微不足道的。

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.引号中的文本需要与任何已配置帐户的真实姓名和 email 地址(人字形)匹配。 If there's no match, Mail.app will use the default address如果没有匹配,Mail.app 将使用默认地址

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.更新:我会查看 Mail 的脚本字典,看看你能做什么,不能做什么。 The information provided there might help.那里提供的信息可能会有所帮助。 :) :)

An alternative to the GUI scripting example I provided would be an Automator script that utilizes the 'Watch Me Do' command. 我提供的 GUI 脚本示例的替代方法是使用“Watch Me Do”命令的 Automator 脚本。 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.我不明白它到底是如何工作的,但它表面上记录了鼠标移动和 GUI 交互,然后在运行时重新制定你记录的内容。 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.要求它运行后,有时会在 5 秒或更长时间后执行,这显然是无法使用的。

The GUI scripting method is almost instantaneous and cleaner-looking (no mouse movement) to boot. GUI 脚本方法几乎可以立即启动并且看起来更干净(没有鼠标移动)。 I recommend it.我推荐它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM