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

使用邮件规则和Applescript将vcard添加到联系人,applescript,apple-mail,Applescript,Apple Mail,我收到很多客户的vCard到特定的电子邮件地址。我想通过邮件规则和AppleScript自动将vCard添加到我的联系人中 我找了很多东西,发现了一些东西。我稍微修改了一下。打开和添加过程也很好。但只有当我选择一个文件时。我无法从邮件消息中将文件放入变量中。我试过了,但没用 以下是我目前的代码: tell application "Mail" set att to attachment end tell set thefile to att tell application

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

我找了很多东西,发现了一些东西。我稍微修改了一下。打开和添加过程也很好。但只有当我选择一个文件时。我无法从邮件消息中将文件放入变量中。我试过了,但没用

以下是我目前的代码:

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,则只能保存附件

最后,但并非最不重要的一点是,似乎在雪豹中运行良好的“保存”指令在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”的文件过滤临时文件夹的内容……以防您选择的电子邮件还包含联系人无法处理的其他类型的文件


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

谢谢-您的脚本对我有效。但是,我将告诉应用程序“联系人”激活打开文件结束告诉应用程序“系统事件”放在哪里“击键返回部分?在剧本的结尾还是中间?没有什么对我有效,但是脚本作为一个独立的脚本可以完美地工作。我只是用完整的脚本更新我的脚本,包括“打开vCard”和删除临时文件夹。非常感谢,但是它从来没有对临时文件夹起作用。我可以将所有文件下载到我的桌面文件夹中,但不能下载到子文件夹中。。。而系统事件击键的事情将不再起作用。。。也许我可以自己修好。。。你在电脑上试过了吗?对不起,是我的错。我在第一个回答中采用了脚本:1)我在旧系统上测试了脚本,其中save-in命令正常。我现在在El Capitain再次测试了它,它现在要求文件条目在“保存”之前存在。这就是我添加“触摸”指令的原因。2) 我没有提到必须创建TempFiles文件夹。所以我在脚本的第2行添加了这个创建…然后您就不需要手动创建它了!非常感谢pbell-你是我的英雄:D,但击键返回事件有时会失败。是否有其他命令自动单击“添加”/“导入”按钮?对不起,如果我什么都问了,哈哈;)我尝试在和处删除文件夹,我建议在尝试删除文件夹之前添加延迟2或3。否则vcard将“损坏”。但其他一切都很完美——再次感谢你