2

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

1 回答 1

0

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

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

此外,如果附件的“已下载”属性为真,您只能保存附件。

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

我还在脚本底部添加了带有回车键的打开 vCard 以在 Contact 中验证。您可能需要延迟一段时间才能让您的计算机处理该卡。

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

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

于 2016-09-16T07:56:03.877 回答