2

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 Answers1

0

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

pbell
  • 2,965
  • 1
  • 16
  • 13
  • Thanks - your script works for me. But where do I put the tell application "Contacts" activate open thefile end tell tell application "System Events" to keystroke return part? At the end of the script or between? Nothing works for me, but the script as a standalone works perfectly. – Christian Mandl Oct 07 '16 at 12:37
  • I just update my script with the complete script, including the "open vCard" and up to temp folder deletion. – pbell Oct 07 '16 at 14:51
  • Thank you so much, but it never worked with the TempFiles folder. I can download all the files into my desktop folder but not in a subfolder ... And the System Events keystroke thing won't work anymore ... Maybe I can fix it on my own ... did you tried it on your computer? – Christian Mandl Oct 09 '16 at 11:58
  • my fault, sorry. I adopted the script in my first answer: 1) I tested the script on old system where the save in command was OK. I now tested it again in El Capitain and it now requires the file entry to exist before the "save". This is why I added the "touch" instruction. 2) I did not mentioned that you must create the TempFiles folder. So I added that creation in line 2 of the script...then you do not need to create it manually ! – pbell Oct 09 '16 at 17:59
  • Thank you so much pbell - you are my hero :D But the keystroke return event fails sometime. Is there any other command(s) to click the "Add"/"Import" button automatically? Sorry if I ask everything haha ;) And I tried it with the folder deletion at the and and I recommend to add an delay 2 or 3 before you try to delete the folder. Otherwise the vcard is "broken". But everything else works perfectly - thank you soo much - again! :) – Christian Mandl Oct 11 '16 at 11:16
  • Unfortunately, there is no applescript instruction to add card into contact. However, I suggest you to add a 'delay 1' instruction between the 'open' command and the 'enter'. I think it could improve... – pbell Oct 11 '16 at 19:27
  • Yes thank you! Now it works haha :D The delay 1 after the open command and the delay 1 before the delete command is very recommended. Thank you so much for your help - you are the best! :) – Christian Mandl Oct 11 '16 at 20:49