2

I automatically receive a lot of vcards via email and I want to import them automatically into my contacts.

I coded an AppleScript that will open the vcard file and import the contact. But first I need to download the file, right?

But how can I download an attachment file from an email using AppleScript and rules in mail?

Thank you.

Cheers, Chris

2 Answers2

1

The script bellow saves the files attached in a selection of emails into a destination folder. you can then use these files to add them in Address Book.

set Dest to ((path to desktop folder) as string) & "FMail:" -- the folder to save attached files
tell application "Mail"
activate
set ListMessage to selection -- take all emails selected
repeat with aMessage in ListMessage -- loop through each message
    set AList to every mail attachment of aMessage
    repeat with aFile in AList --loop through each files attached to an email
        if (downloaded of aFile) then -- check if file is already downloaded
            set Filepath to Dest & (name of aFile)
            save aFile in Filepath as native format
        end if
    end repeat -- next file
end repeat -- next message
end tell

I added many comments to make it clear. then you will be able to adapt it to your needs.

pbell
  • 2,965
  • 1
  • 16
  • 13
  • Solved the problem here: [link](http://stackoverflow.com/questions/39509518/add-vcard-to-contacts-with-mail-rules-and-applescript) Thanks to you @pbell – Christian Mandl Oct 11 '16 at 20:54
1

For Mail Version 8.2 (2104) in macOS 10.10.5, you need to download to the download folder. Thus, I change the first line of pbell's solution to

set Dest to ((path to home folder) as string) & "Downloads:" -- the folder to save attached files

(*
Mail Version 8.2 (2104)  year 2015
macOS 10.10.5
*)

set Dest to ((path to home folder) as string) & "Downloads:" -- the folder to save attached files
log "Dest is " & Dest


tell application "Mail"
    activate
    set ListMessage to selection -- take all emails selected
    repeat with aMessage in ListMessage -- loop through each message
        set AList to every mail attachment of aMessage
        repeat with aFile in AList --loop through each files attached to an email
            if (downloaded of aFile) then -- check if file is already downloaded
                set Filepath to Dest & (name of aFile)
                log "Filepath is " & Filepath
                save aFile in Filepath as native format
            end if
        end repeat -- next file
    end repeat -- next message
end tell
rccharles
  • 11
  • 1