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

2024-03-28

我通过特定的电子邮件地址收到了很多客户电子名片。我想通过邮件规则和 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 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

从电子邮件附加的文件响应“保存”命令,但不响应“打开”命令。然后,您必须首先保存附件,然后将它们移动到下一个应用程序(在您的情况下添加到“联系人”中)。

附件是消息的“邮件附件”列表的成员:请记住,可能会附加许多文件。

此外,只有“已下载”属性为 true 时,您才能保存附件。

最后但并非最不重要的一点是,似乎在 Snow Leopard 中运行良好的“保存”指令在 El Capitain 中的运行效果并不相同:要保存的文件必须在“保存”之前存在......这就是为什么我添加了“touch”命令来首先创建它(只需在 tempFiles 文件夹中创建条目)。

我还在脚本底部添加了打开的 vCard,并使用 Enter 键在联系人中进行验证。您可能需要延迟一段时间,让计算机处理该卡。

如果按键损坏在您的情况下不起作用,请检查“系统偏好设置”辅助功能设置,以允许您的计算机让脚本控制您的 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”的文件来过滤临时文件夹的内容...以防万一您选择的电子邮件还包含“联系人”无法处理的其他类型的文件。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用邮件规则和 Applescript 将 vcard 添加到联系人 的相关文章

随机推荐