简体   繁体   English

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

[英]Last sent e-mail from Mail.app with Applescript

Is there any way to get the last sent e-mail from Mail? 有什么方法可以从Mail获取上次发送的电子邮件? It can only access the top level sent mailbox. 它只能访问顶级已发送邮箱。

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

The reason I need this is that I want to get information about the e-mail that is beeing written. 我之所以需要它,是因为我想获得有关正在撰写的电子邮件的信息。 My thought was to get the frontmost window. 我的想法是获得最前面的窗口。

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

It is working for the name but I do not get the sender or receipient. 它适用于该名称,但我不知道发件人或收件人。 Any ideas? 有任何想法吗?

As said, you just have to loop through each account, and each time, compare the date of the email with previous email date from other account. 如前所述,您只需要遍历每个帐户,然后每次将电子邮件的日期与其他帐户的先前电子邮件日期进行比较。 here is the script : (I also add a test in case the sent box of one account is empty !) 这是脚本:(如果一个帐户的发送箱为空,我还会添加一个测试!)

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

Unfortunately the messages in the sent mailbox are unsorted in the AppleScript array. 不幸的是,已发送邮箱中的消息在AppleScript数组中未排序。

You can use this code, but it could take a while depending on the number of messages in the mailbox (ca. 30 sec for 1000 messages). 您可以使用此代码,但是可能要花一些时间,具体取决于邮箱中的邮件数量(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

The code bellow uses to work for me in all my Mail versions up to 4.6 (Snow Leopard). 下面的代码可用于我所有的4.6(Snow Leopard)邮件版本中。 To be tested on later versions. 在更高版本上进行测试。 Strangely, "last message" gives the oldest message in the mailbox and the "first message" gives the last message sent or received ! 奇怪的是,“最后一条消息”给出了邮箱中最早的消息,而“第一条消息”给出了发送或接收的最后一条消息! if you have many accounts, just loop on accounts names : 如果您有许多帐户,只需循环输入帐户名称:

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

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

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