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

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

问题描述

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

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

Here is my code so far:

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.

此外,只有在附件的已下载"属性为true时,您才能保存附件.

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

最后但并非最不重要的一点是,似乎在Snow Leopard中运行良好的"save"指令在El Capitain中无法正常工作:要保存的文件必须在"save"之前存在...此这就是为什么我添加"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).

我还在脚本底部添加了带有Enter键的开放vCard,以在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'的文件过滤temp文件夹的内容...以防万一您选择的电子邮件中还包含Contact无法处理的其他类型的文件.

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.

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

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 !)

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

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