添加电子名片到邮件规则和Applescript的联系人

问题描述:

我收到大量的客户电子贺卡到特定的电子邮件地址。我想通过邮件规则和AppleScript自动将vcards添加到我的联系人。添加电子名片到邮件规则和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行“设置thefile选择文件”,那么它会工作写 - 如果让我选择一个文件。 但前三行我尝试了一些东西,但没有任何成功。 所以我的问题是,我怎样才能从消息中获取文件?

谢谢

此致, 克里斯

解决方案:

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的情况下,才能保存附件。

最后但并非最不重要的一点是,似乎在“雪豹”中运行良好的“保存”指令在El Capitain中不起作用:要保存的文件在“保存”之前必须存在...这就是为什么我添加了“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”文件的内容...以防万一您选择的电子邮件中也含有该联系人无法处理其他类型的文件。

在脚本结尾,我删除了临时文件夹。然而,直到你测试它,我把这最后一行作为评论(更安全!)

+0

谢谢 - 你的脚本适合我。 但我在哪里把 告诉应用程序“联系人” 激活 开放thefile 端告诉 告诉应用程序“系统事件”向按键返回 一部分?在剧本结束还是之间?没有任何作品适合我,但脚本作为独立作品完美无缺。 –

+0

我只是用完整的脚本更新我的脚本,包括“打开电子名片”,并删除临时文件夹。 – pbell

+0

非常感谢你,但它从来没有与TempFiles文件夹一起工作。 我可以下载所有的文件到我的桌面文件夹,但不是在子文件夹中... 而系统事件击键的事情将不再工作... 也许我可以解决它在我自己...你试过它在你的电脑上? –