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

时间:2011-07-10 02:06:11

标签: macos applescript apple-mail

我想编写一个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

当我尝试按如下方式询问前方外播消息时:

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

我得到{class:item}作为回报,而不是像我期望的那样的“传出消息”对象。

有什么想法吗?

7 个答案:

答案 0 :(得分:5)

不幸的是,you cannot get or set the properties of the outgoing message object of Mail with Applescript.

相反,您可以使用GUI脚本来完成此操作,这是一种直接操作窗口元素的解决方法。

此代码适用于您:

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 6menu item 4)。


更新:如果你很好奇,因为这个答案已经超过两年了:截至2014-04-26,获取/设置outgoing message s的属性仍然是不可能的此解决方法仍适用于OS X 10.9 Mavericks。

答案 1 :(得分:3)

您无法设置外发邮件的发件人,这有点令人沮丧。

这是我多年前写过并经常使用的马修剧本的更一般版本。参数是一个字符串,其中包含您在“发件人:”弹出窗口中看到的内容,例如

"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

我使用参数化处理程序的原因是我使用按键宏功能来绑定每个电子邮件地址的按键组合。每个执行AppleScript,加载包含上述处理程序的文件,并使用特定的电子邮件地址调用它。例如:

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>")

答案 2 :(得分:2)

我觉得它有点复杂。我自己的实验同意Alan Kimelman在this thread中的结论,即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的破坏是正确的。您无法设置或获取任何属性,也拒绝发送命令。

答案 3 :(得分:2)

实际上,您可以按照此处所述设置外发邮件的发件人: Incorrect sender when sending Email via Applescript

答案 4 :(得分:1)

使用AppleScript创建一个包含您喜欢的任何外发地址的新外发邮件是非常简单的(至少,在您的Mail.app首选项中配置的地址)。

此处的讨论主要围绕在创建消息后尝试(错误地)更改它。而是在创建消息时指定它:

set new_m to make new outgoing message with properties {sender:"My Name <me@my.com>"}

引号中的文字需要与任何已配置帐户的真实姓名和电子邮件地址(在V形图中)相匹配。如果没有匹配,Mail.app将使用默认地址

答案 5 :(得分:0)

试试这个。

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

更新:我会查看Mail的脚本词典,看看你能做什么,不能做什么。这里提供的信息可能有所帮助。 :)

答案 6 :(得分:0)

the GUI scripting example I provided的替代方案是使用“Watch Me Do”命令的Automator脚本。我不明白它是如何在引擎盖下工作的,但它表面上记录了鼠标移动和GUI交互,然后重新演绎它在运行时记录的内容。不幸的是,我在运行脚本时遇到了严重的延迟。在要求它运行之后,它有时会在5秒或更长时间后执行,这显然无法使用。

GUI脚本编写方法几乎是即时的,外观更清晰(无鼠标移动)。我推荐它。

相关问题