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

时间:2016-10-05 19:39:23

标签: email applescript macos-sierra

我会通过电子邮件自动收到很多vcards,我想将它们自动导入我的联系人。

我编写了一个AppleScript,它将打开vcard文件并导入联系人。但首先我需要下载文件,对吗?

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

谢谢。

干杯, 克里斯

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

对于macOS 10.10.5中的Mail版本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