使用邮件规则和 Applescript 将 vcard 添加到联系人

Add vcard to Contacts with Mail rules and Applescript

我在一个特定的电子邮件地址收到了很多客户 vcard。我想通过邮件规则和 AppleScript 将电子名片自动添加到我的联系人中。

我搜索了很多,找到了一些东西。我修改了一下。打开和添加过程工作正常。但只有当我选择一个文件时。我无法从邮件消息中将文件放入变量中。我试过了,但是不行。

到目前为止,这是我的代码:

tell application "Mail"  
  set att to attachment  
end tell  
set thefile to att  
tell application "Contacts"  
  activate  
  open thefile  
end tell  
tell application "System Events" to keystroke return

如果我删除第 1、2 和 3 行并在第 4 行写入 "set thefile to choose file" 那么它将起作用 - 如果我选择一个文件。 但是前三行我尝试了一些东西,但没有成功。 所以我的问题是,如何从消息中获取文件?

谢谢

您真诚的, 克里斯

解法:

set Dest to ((path to desktop folder) as string) 
tell application "Finder" to make new folder in Dest with properties {name:"TempFiles"} -- create TempFiles folder
Set Dest to Dest & "TempFiles:" 
tell application "Mail"
activate -- not sure is mandatory, but I prefer to see selected mails !!
set ListMessage to selection -- get all selected messages
repeat with aMessage in ListMessage -- loop through each message selected
    set AList to every mail attachment of aMessage -- get all attachements
    repeat with aFile in AList -- for each attachement
        if (downloaded of aFile) then
            set Filepath to Dest & (name of aFile)
            do shell script "touch " & (quoted form of (POSIX path of Filepath)) -- required because "Save" only works with existing file !
            save aFile in (Filepath as alias) as native format
        end if
    end repeat -- next file
end repeat -- next message
end tell

tell application "Finder" to set CardList to every file of folder Dest whose name extension is {"vcf"}
tell application "Contacts"
activate
repeat with aCard in CardList
    open aCard
    delay 1
    tell application "System Events" to keystroke return
end repeat
end tell
delay 2
-- tell application "Finder" to delete folder Dest

邮件附件响应“保存”命令,但不响应'open'。然后,您必须先保存附件,然后将它们移动到下一个应用程序(添加在'Contacts' 在你的情况下)。

附件是邮件 'mail attachement' 列表的成员:请记住,附件可能有很多。

此外,只有 'downloaded' 属性为真,您才能保存附件。

最后但并非最不重要的一点是,在 Snow Leopard 中运行良好的 "save" 指令似乎在 El Capitain 中无法正常工作:要保存的文件必须存在于 "save"...这就是我添加 "touch" 命令首先创建它的原因(只需在 tempFiles 文件夹中创建条目)。

我还在脚本底部添加了带有回车键的打开的 vCard,以在联系人中进行验证。您可能需要延迟一段时间才能让您的计算机处理卡片。

如果坏掉的键在您的情况下不起作用,请检查系统偏好设置的辅助功能设置,以允许您的计算机让您的脚本控制您的 Mac。

我添加了尽可能多的评论以使其清楚...可能太多了!

set Dest to ((path to desktop folder) as string) 
tell application "Finder" to make new folder in Dest with properties {name:"TempFiles"} -- create TempFiles folder
Set Dest to Dest & "TempFiles:" 
tell application "Mail"
activate -- not sure is mandatory, but I prefer to see selected mails !!
set ListMessage to selection -- get all selected messages
repeat with aMessage in ListMessage -- loop through each message selected
    set AList to every mail attachment of aMessage -- get all attachements
    repeat with aFile in AList -- for each attachement
        if (downloaded of aFile) then
            set Filepath to Dest & (name of aFile)
            do shell script "touch " & (quoted form of (POSIX path of Filepath)) -- required because "Save" only works with existing file !
            save aFile in (Filepath as alias) as native format
        end if
    end repeat -- next file
end repeat -- next message
end tell

tell application "Finder" to set CardList to every file of folder Dest whose name extension is {"vcf"}
tell application "Contacts"
activate
repeat with aCard in CardList
    open aCard
    tell application "System Events" to keystroke return
end repeat
end tell

-- tell application "Finder" to delete folder Dest

如您所见,我仅使用扩展名为 'vcd' 的文件过滤临时文件夹的内容...以防万一您选择的电子邮件还包含 Contact 无法处理的其他类型的文件。

在脚本结束时,我删除了临时文件夹。但是,在您对其进行测试之前,我将最后一行设置为仅作为注释(更安全!)