Christian Mandl Christian Mandl - 1 month ago 20
AppleScript Question

How to download an attachment from mail via applescript and mail rules?

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

Answer

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.