使用邮件规则和 Applescript 将 vcard 添加到联系人 [英] Add vcard to Contacts with Mail rules and Applescript

查看:31
本文介绍了使用邮件规则和 Applescript 将 vcard 添加到联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I receive a lot of customer vcards to a specific email address. I want to automatically add the vcards to my contacts through the Mail rules and an AppleScript.

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

I searched a lot and found something. I modified it a bit. And the opening and adding process works fine. But only when I choose a file. I can't get the file into a variable from the mail message. I tried it but it won't work.

这是我目前的代码:

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 行中写入设置文件以选择文件",那么它将起作用 - 如果我选择一个文件.但是前三行我尝试了一些东西,但没有任何成功.所以我的问题是,如何从消息中获取文件?

If I delete line 1, 2 and 3 and write in line 4 "set thefile to choose file" then it will work - if I choose a file. But the first three lines I tried something out, but without any success. So my question is, how can I get the file from the message?

谢谢

真诚的,克里斯

解决方案:

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

推荐答案

邮件附件响应保存"命令,但不响应打开".然后,您必须先保存附件,然后,将它们移至下一个应用程序(在您的情况下添加联系人").

The file attached from email respond to the 'save" command, but not to 'open'. Then, you must first save the attached files, and later, move them to next application (add in 'Contacts' in your case).

附件是邮件邮件附件"列表的成员:请记住,可能会附加许多文件.

The attachement is a member of a list of 'mail attachement' of a message : keep in mind that there could be many files attached.

此外,您只能保存已下载"属性为真的附件.

Also, you can only save the attached file if its 'downloaded' attribute is true.

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

Last, but not least, it seems that the "save" instruction which was working nice in Snow Leopard, does not work the same in El Capitain : the file where to save must exist before the "save"...this is why I added the "touch" command to create it first (just create the entry in the tempFiles folder).

我还在脚本底部添加了打开的 vCard,并使用 Enter 键在 Contact 中进行验证.您可能需要添加一个延迟,以便为您的计算机留出一些时间来处理该卡.

I also add at bottom of script the open vCard with the enter key to validate in Contact. You may have to add a delay to leave sometime for your computer to process the card.

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

if the keys broke does not work in your case, please check System Preferences accessibility settings to allow your computer to let your script control your Mac.

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

I added as much comments as possible to make it clear...may be too much !

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"的文件过滤临时文件夹的内容……以防万一您选择的电子邮件还包含联系人无法处理的其他类型的文件.

As you can see, I filter the content of the temp folder with only files with extension 'vcd'...just in case your selected emails contain also other type of file which Contact can't handled.

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

At end of the script, I delete the temp folder. however, until you test it, I set this last row as a comment only (more safe !)

这篇关于使用邮件规则和 Applescript 将 vcard 添加到联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆