如何通过 applescript 和邮件规则从邮件下载附件?

How to download an attachment from mail via applescript and mail rules?

我通过电子邮件自动收到很多电子名片,我想将它们自动导入到我的通讯录中。

我编写了一个 AppleScript 代码,可以打开 vcard 文件并导入联系人。但首先我需要下载文件,对吧?

但是如何使用 AppleScript 和邮件中的规则从电子邮件下载附件文件?

谢谢。

干杯, 克里斯

下面的脚本将所选电子邮件中的附件保存到目标文件夹中。然后您可以使用这些文件将它们添加到地址簿中。

set Dest to ((path to desktop folder) as string) & "FMail:" -- the folder to save attached files
tell application "Mail"
activate
set ListMessage to selection -- take all emails selected
repeat with aMessage in ListMessage -- loop through each message
    set AList to every mail attachment of aMessage
    repeat with aFile in AList --loop through each files attached to an email
        if (downloaded of aFile) then -- check if file is already downloaded
            set Filepath to Dest & (name of aFile)
            save aFile in Filepath as native format
        end if
    end repeat -- next file
end repeat -- next message
end tell

我添加了很多评论以使其清楚。然后您就可以根据自己的需要进行调整。

对于 macOS 10.10.5 中的邮件版本 8.2 (2104),您需要下载到下载文件夹。因此,我将 pbell 解决方案的第一行更改为

set Dest to ((path to home folder) as string) & "Downloads:" -- the folder to save attached files

(*
Mail Version 8.2 (2104)  year 2015
macOS 10.10.5
*)

set Dest to ((path to home folder) as string) & "Downloads:" -- the folder to save attached files
log "Dest is " & Dest


tell application "Mail"
    activate
    set ListMessage to selection -- take all emails selected
    repeat with aMessage in ListMessage -- loop through each message
        set AList to every mail attachment of aMessage
        repeat with aFile in AList --loop through each files attached to an email
            if (downloaded of aFile) then -- check if file is already downloaded
                set Filepath to Dest & (name of aFile)
                log "Filepath is " & Filepath
                save aFile in Filepath as native format
            end if
        end repeat -- next file
    end repeat -- next message
end tell