【问题标题】:How to set the sender of the current Mail.app outgoing message via AppleScript?如何通过 AppleScript 设置当前 Mail.app 外发消息的发件人?
【发布时间】:2011-10-02 02:17:14
【问题描述】:

我想编写一个 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} 作为回报,而不是像我期望的那样“传出消息”对象。

有什么想法吗?

【问题讨论】:

    标签: macos applescript apple-mail


    【解决方案1】:

    试试这个。

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

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

    【讨论】:

    • 谢谢,但我想更改当前邮件的发件人,而不是创建新邮件。
    【解决方案2】:

    很遗憾,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 6 更改为menu item 4 )。


    更新:如果你好奇,因为这个答案已经有两年多了:截至 2014 年 4 月 26 日,获取/设置 outgoing messages 的属性仍然是不可能的,这解决方法在 OS X 10.9 Mavericks 中仍然有效。

    【讨论】:

    • 我会补充一点,如果您尝试设置的电子邮件地址不在该列表中,您可以轻松地将其添加到列表中。在“邮件”偏好设置的“帐户”下的“帐户信息”面板下,添加您希望能够使用该帐户发送邮件的电子邮件地址,以逗号分隔。
    【解决方案3】:

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

    GUI 脚本方法几乎​​可以立即启动并且看起来更干净(无需鼠标移动)。我推荐它。

    【讨论】:

      【解决方案4】:

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

      这是我多年前编写并经常使用的 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
      

      我使用参数化处理程序的原因是我使用击键宏工具为我的每个电子邮件地址绑定击键组合。每个都执行一个 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>")
      

      【讨论】:

      • 太好了,谢谢。 @Matthew 的回答在回复设置了“高优先级”标志的消息时不起作用,因为弹出按钮编号已更改。但是使用短语pop up button "From:" 无论如何都有效。
      【解决方案5】:

      我认为这有点复杂。我自己的实验同意 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 已损坏是正确的。您无法设置或获取任何属性,并且它也拒绝 send 命令。

      【讨论】:

      • 您必须确保发件人姓名与下拉菜单中的姓名完全相同才能正常工作。
      【解决方案6】:

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

      【讨论】:

        【解决方案7】:

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

        这里的讨论围绕在创建消息后尝试(错误地)更改它。相反,请在创建消息时指定它:

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

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

        【讨论】:

          猜你喜欢
          • 2018-06-26
          • 1970-01-01
          • 2013-07-29
          • 1970-01-01
          • 2011-11-10
          • 1970-01-01
          • 1970-01-01
          • 2012-01-01
          • 2018-04-29
          相关资源
          最近更新 更多