上次使用 Applescript 从 Mail.app 发送电子邮件

Last sent e-mail from Mail.app with Applescript

有什么方法可以从Mail 中获取上次发送的电子邮件吗? 它只能访问顶级发送邮箱。

tell application "Mail"
    set mabo to the sent mailbox
    set selected to messages of mabo
    return properties of item 1 of selected
end tell

我需要这个的原因是我想获得有关正在编写的电子邮件的信息。我的想法是得到最前面的 window.

tell application "Mail"
    set the win to windows
    set theMessage to item 1 of win
    get name of theMessage
end tell

它正在为这个名字工作,但我没有得到发件人或收件人。有什么想法吗?

很遗憾,已发送邮箱中的邮件在 AppleScript 数组中未排序。

您可以使用此代码,但可能需要一段时间,具体取决于邮箱中的邮件数量(1000 封邮件大约需要 30 秒)。

tell application "Mail"
    set sentMessages to messages of sent mailbox
    set lastestMessage to item 1 of sentMessages
    set latestDate to date received of lastestMessage

    repeat with aMessage in sentMessages
        if date received of contents of aMessage comes after latestDate then
            copy contents of aMessage to lastestMessage
            set latestDate to date received of lastestMessage
        end if
    end repeat
    tell lastestMessage
        set theSender to sender
        set theRecipient to 1st recipient
    end tell
end tell

下面的代码适用于我所有的邮件版本,最高为 4.6 (Snow Leopard)。待后续版本测试。奇怪的是,"last message" 给出了邮箱中最早的消息,而 "first message" 给出了最后发送或接收的消息!如果您有很多帐户,只需循环帐户名称即可:

tell application "Mail"
-- to get the last message sent
set X to first message of mailbox "Sent Messages" of account "your_account_name"
-- to get the oldest message received in receiving box
set X to last message of mailbox "INBOX" of account "your_account_name"
open X --if want to see it
end tell

如前所述,您只需遍历每个帐户,每次都将电子邮件的日期与其他帐户的先前电子邮件日期进行比较。 这是脚本:(我还添加了一个测试,以防一个帐户的发件箱为空!)

tell application "Mail"
set LastDate to date "samedi 1 janvier 2000 00:00:00"
set {myLastMail, myAccount, myDate} to {"", "", LastDate}
repeat with CAccount in every account
    if (count of every message in mailbox "Sent Messages" of CAccount) > 0 then
        set X to first message of mailbox "Sent Messages" of CAccount
        set MDate to date sent of X
        if MDate > LastDate then
            set LastDate to MDate
            set {myLastMail, myAccount, myDate} to {X, CAccount, LastDate}
        end if
    end if
end repeat
-- the last email from all your accounts is the variables  Mylastemail, in account MyAccount and at the date MyDate
end tell