![](/img/trans.png)
[英]Applescript Mail.app “get every outgoing message” only showing messages created by script
[英]How to set the sender of the current Mail.app outgoing message via AppleScript?
我想写一个 AppleScript 在 Apple 的 Mail.app 中设置当前外发邮件的发件人。
我试过这个:
tell application "Mail" to set sender of front outgoing message to "<my email address>"
但我收到错误消息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
。
当我尝试如下询问前面的传出消息时:
tell application "Mail" to get properties of front outgoing message
我得到{class:item}
作为回报,而不是我期望的“传出消息” object 。
有任何想法吗?
不幸的是,您无法获取或设置带有 Applescript 的 Mail 的outgoing message
object 的属性。
相反,您可以使用 GUI 脚本来完成此操作,这是一种直接操作 window 元素的解决方法。
此代码应该适合您:
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
将第四行的menu item 6
更改为列表中您想要的发件人的任何数字(例如,如果您要使用此脚本更改的发件人是列出的第四个,则将menu item 6
更改为menu item 4
)。
更新:如果你很好奇,因为这个答案已经超过两年了:截至 2014-04-26,获取/设置outgoing message
的属性仍然是不可能的,并且这种解决方法仍然适用于 OS X 10.9 Mavericks。
您无法设置外发邮件的发件人,这有点令人沮丧。
这是我多年前编写并经常使用的 Matthew 脚本的更通用版本。 参数是一个字符串,其中包含您在“From:”弹出窗口中看到的内容,例如
"Mitchell L Model <dev@software-concepts.org>"
(如果您不编写大量 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
我使用参数化处理程序的原因是我使用击键宏工具为我的每个 email 地址绑定击键组合。 每个执行一个 AppleScript 加载包含上述处理程序的文件并使用特定的 email 地址调用它。 例如:
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>")
我认为这有点复杂。 我自己的实验同意 Alan Kimelman 在 此线程中的结论,即 AppleScript对于脚本从头开始创建的传出消息按预期工作。 例如,以下代码有效:
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
但是,如果消息是通过其他方式创建的(例如,我通过告诉 Safari 通过电子邮件共享来创建 HTML 消息),那么 Matthew McVickar 是正确的 AppleScript 已损坏。 您无法设置或获取任何属性,并且它也拒绝发送命令。
实际上,您可以按此处所述设置传出消息的发件人: Incorrect sender when sent Email via Applescript
使用 AppleScript 使用您喜欢的任何外发地址创建新的外发邮件(至少,在您的 Mail.app 首选项中配置的地址)是微不足道的。
这里的讨论围绕在创建消息后尝试(错误地)更改它。 相反,在创建消息时指定它:
set new_m to make new outgoing message with properties {sender:"My Name <me@my.com>"}
引号中的文本需要与任何已配置帐户的真实姓名和 email 地址(人字形)匹配。 如果没有匹配,Mail.app 将使用默认地址
试试这个。
tell application "Mail" to make new outgoing message with properties {sender:"<your_email_address>", visible:true}
更新:我会查看 Mail 的脚本字典,看看你能做什么,不能做什么。 那里提供的信息可能会有所帮助。 :)
我提供的 GUI 脚本示例的替代方法是使用“Watch Me Do”命令的 Automator 脚本。 我不明白它到底是如何工作的,但它表面上记录了鼠标移动和 GUI 交互,然后在运行时重新制定你记录的内容。 不幸的是,我在运行脚本时遇到了明显的延迟。 要求它运行后,有时会在 5 秒或更长时间后执行,这显然是无法使用的。
GUI 脚本方法几乎可以立即启动并且看起来更干净(没有鼠标移动)。 我推荐它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.