Add vcard to Contacts with Mail rules and Applescript

你离开我真会死。 提交于 2019-12-12 18:56:23

问题


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

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?

Thank you

Yours sincerely, Chris

Solution:

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

回答1:


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.

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

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.

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

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



来源:https://stackoverflow.com/questions/39509518/add-vcard-to-contacts-with-mail-rules-and-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!