获取草稿的收件人(AppleScript、Mail.app)

发布于 2024-12-16 15:51:58 字数 82 浏览 0 评论 0原文

我想使用 AppleScript 获取 Mail.app 草稿文件夹中草稿的收件人(“收件人”字段等)。似乎找不到正确的语法。

谢谢。

I want to get the recipients ("to" fields, etc) of a draft on my draft's folder of Mail.app, by using AppleScript. Can't seem to find the correct syntax to that.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寻找我们的幸福 2024-12-23 15:51:59

实际上,与之前提出的 Cocoa 解决方案不同,纯 AppleScript 是可能的。以下是用于获取“TO”收件人字段的输入值的代码片段。

tell application "Mail"
    set draftMessages to every message in drafts mailbox
    set draftMessagesID to {}

    # go through each draft message
    repeat with draftMessage in draftMessages
         set draftMessageID to id of draftMessage as string
         copy draftMessageID to the end of draftMessagesID
    end repeat

    # go through the list of draft message ids and process the most recent item
    if (count of the draftMessagesID) is greater than 1 then
        set sortedDraftMessagesID to the reverse of my sortAlphabetically(the draftMessagesID)
        # get only the first item, as this is the most recent
        set lastDraftMessageID to first item of sortedDraftMessagesID as integer
        # get the most recent draft message
        set draftMessage to first message of drafts mailbox whose id is lastDraftMessageID

        set toAddresees to {}
        repeat with toRecipient in (get to recipients of draftMessage)
            set toName to name of toRecipient
            set toAddress to address of toRecipient
            set toFinal to my composeNameAndAddress(toName, toAddress)
            copy toFinal to end of toAddresees
        end repeat

        # now you have the input values of the TO field
        log toAddresees
    end if
end tell

#handler to compose name and address when one is missing
on composeNameAndAddress(name, address)
    if name is missing value then
        return address
    else
        return name & space & "<" & address & ">"
    end if
end composeNameAndAddress

#handler to sort a list alphabetically
on sortAlphabetically(theList)
    set the indexList to {}
    set the sortedList to {}
    repeat (the number of items in theList) times
        set the lowItem to ""
        repeat with i from 1 to (number of items in theList)
            if i is not in the indexList then
                set thisItem to item i of theList as string
                if the lowItem is "" then
                    set the lowItem to thisItem
                    set the lowItemIndex to i
                else if thisItem comes before the lowItem then
                    set the lowItem to thisItem
                    set the lowItemIndex to i
                end if
            end if
        end repeat
        set the end of sortedList to the lowItem
        set the end of the indexList to the lowItemIndex
    end repeat
    return the sortedList
end sortAlphabetically   

您提到您需要一个特定的草稿,对于此脚本,我假设您可以(例如)通过获取具有最高 ID 的草稿消息来获取最新草稿。这就是上面的脚本使用带有通用简单排序处理程序的反向命令所做的事情。

Actually it is possible with pure AppleScript unlike the Cocoa solution proposed earlier. Here's a snippet of the code used to get the input values of the "TO" recipients field.

tell application "Mail"
    set draftMessages to every message in drafts mailbox
    set draftMessagesID to {}

    # go through each draft message
    repeat with draftMessage in draftMessages
         set draftMessageID to id of draftMessage as string
         copy draftMessageID to the end of draftMessagesID
    end repeat

    # go through the list of draft message ids and process the most recent item
    if (count of the draftMessagesID) is greater than 1 then
        set sortedDraftMessagesID to the reverse of my sortAlphabetically(the draftMessagesID)
        # get only the first item, as this is the most recent
        set lastDraftMessageID to first item of sortedDraftMessagesID as integer
        # get the most recent draft message
        set draftMessage to first message of drafts mailbox whose id is lastDraftMessageID

        set toAddresees to {}
        repeat with toRecipient in (get to recipients of draftMessage)
            set toName to name of toRecipient
            set toAddress to address of toRecipient
            set toFinal to my composeNameAndAddress(toName, toAddress)
            copy toFinal to end of toAddresees
        end repeat

        # now you have the input values of the TO field
        log toAddresees
    end if
end tell

#handler to compose name and address when one is missing
on composeNameAndAddress(name, address)
    if name is missing value then
        return address
    else
        return name & space & "<" & address & ">"
    end if
end composeNameAndAddress

#handler to sort a list alphabetically
on sortAlphabetically(theList)
    set the indexList to {}
    set the sortedList to {}
    repeat (the number of items in theList) times
        set the lowItem to ""
        repeat with i from 1 to (number of items in theList)
            if i is not in the indexList then
                set thisItem to item i of theList as string
                if the lowItem is "" then
                    set the lowItem to thisItem
                    set the lowItemIndex to i
                else if thisItem comes before the lowItem then
                    set the lowItem to thisItem
                    set the lowItemIndex to i
                end if
            end if
        end repeat
        set the end of sortedList to the lowItem
        set the end of the indexList to the lowItemIndex
    end repeat
    return the sortedList
end sortAlphabetically   

You mentioned that you needed a specific draft, for this script I've assumed that you can (for instance) obtain the latest draft by taking the draft message with the highest ID. This is what the above script does using the reverse command with a generic simple sort handler.

在梵高的星空下 2024-12-23 15:51:59

草稿邮箱包含在消息查看器中。由于邮件尚未发送,因此您无法获取其“收件人”。但是,您可以获取包含收件人列表的消息源。诀窍是从该文本中提取收件人。

tell application "Mail"
set the_messages to every message in drafts mailbox
repeat with this_message in the_messages
    set message_content to the source of this_message
    log message_content
end repeat
end tell

The drafts mailbox is contained by the message viewer. Since the messages have not been sent yet, you cannot get its "to recipients". However, you can get the message source that contains the list of recipients. The trick is to then extract the recipients from this text.

tell application "Mail"
set the_messages to every message in drafts mailbox
repeat with this_message in the_messages
    set message_content to the source of this_message
    log message_content
end repeat
end tell
南笙 2024-12-23 15:51:58

下面是在 Scripting Bridge (Cocoa) 中执行此操作的代码:

for (MailRecipient *recp in message.recipients) {
                MailToRecipient *theRecipient = [[[mail classForScriptingClass:@"to recipient"] alloc] initWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:recp.address, @"address", nil]];
            }

Here's code to do it, in Scripting Bridge (Cocoa):

for (MailRecipient *recp in message.recipients) {
                MailToRecipient *theRecipient = [[[mail classForScriptingClass:@"to recipient"] alloc] initWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:recp.address, @"address", nil]];
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文