使用 AppleScript / JXA 在 Apple Mail 中获取电子邮件 URL

问题描述

可以通过多个 Mac OS 应用程序的唯一 URL 访问 Apple Mail 中的特定电子邮件。 URL 的格式为:message://%3c%3e,其中 是电子邮件的 ID。

我想要一个 AppleScript(或 JXA)脚本,当我在 Apple Mail 中并选择了几条消息时,它会为当前选择的每条消息生成这样的 URL,并将这些 URL 放在剪贴板中,以换行符分隔,允许我将它们粘贴到其他应用程序中。

这是我到目前为止所拥有的,实现了这一点:

tell application "Mail"
    set _sel to selection
    set _links to {}
    repeat with _msg in _sel
        -- Workaround for getting the actual Message-ID. This is necessary in situations when this is a redirected message. For such redirected messages,Apple Mail sets the "message id" to "Redirect-Message-ID". However,the URL will not be recognised by Apple Mail by "Redirect-Message-ID",only by "Message-ID".
        tell _msg
            set headerText to all headers
        end tell
        set parsedText to do shell script "egrep ^Message-Id: <<< " & quoted form of headerText
        set AppleScript's text item delimiters to "Message-Id: <"
        set parsedText to text item 2 of parsedText
        set AppleScript's text item delimiters to ">"
        set parsedText to text item 1 of parsedText
        
        -- Generate Markdown format URL. Looks like [subject](URL to message).
        set _messageURL to "[" & subject of _msg & "](message://%3c" & parsedText & "%3e)"
        set end of _links to _messageURL
    end repeat
    
    -- For some reason,it sometimes happens to have duplicates. Deduplicate here.
    set AppleScript's text item delimiters to character id 10
    set theString to _links as string
    set AppleScript's text item delimiters to ""
    
    set sortedList to every paragraph of (do shell script "/bin/echo -n " & quoted form of theString & "| sort -u")
        
    set AppleScript's text item delimiters to return
    set the clipboard to (sortedList as string)
    --return (_links as string)
end tell

这有效,但是我遇到的最大问题如下。有时我已经回复了特定的电子邮件。如果是这样,我希望 URL 指向我的回复,以提醒我我已经回复了。当然,我的回复正文也会包含原始电子邮件。我不知道该怎么做。我需要做类似的事情:

  • 假设收件箱中当前选中的邮件是 X;
  • 查看所有回复给 X 的电子邮件的所有已发送消息,可能会首先按最新回复排序;
  • 检索所有这些回复的 Message-ID,而不是 X 的 Message-ID。

其他问题:

  1. 注意为重定向消息提取消息 ID 的丑陋解决方法在这方面有什么建议吗?
  2. 注意丑陋的重复数据删除。有什么建议吗?
  3. 我很想在 JXA 中做到这一点,但我不知道从哪里开始!我知道这是一个未详细说明的问题,请随意忽略它。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)