Using POSIX Path to Attach File to Email in AppleScript

applescriptmacosmail.app

I am attempting to send an email via the standard Apple Mail application using a POSIX file reference on macOS Sierra (10.12.3). Everything builds fine, but when it sends, the attachment is not being received.

Here's the code:

set fileReference to (choose file with prompt "Select file for attachment...")

tell application "Mail"
make new attachment with properties {file name:fileReference}

The recipient is set earlier in the code, and there are no issues with sending the email itself: just the attachment.

Any help or insight would be greatly appreciated.

Best Answer

  1. Open Script Editor, create a new document, and add the AppleScript Code, shown below, to it.

  2. In the make new to recipient ... line of code, change the values of {name:"John Doe", address:"johndoe@domain.com"} to your name and email address for testing purposes.

  3. Now run the script.

    • Note that after selecting the attachment, the script will compose the email, attach the file, and send it.
    • Then check your Inbox for the message, to see that the file was attached.
    • It worked for me!

AppleScript Code:

set theAttachment to (choose file with prompt "Select file for attachment...")
tell application "Mail"
    set theMessage to make new outgoing message with properties {visible:true, subject:"File Attachment Test", content:"Was a file attached when you received this email?" & linefeed & linefeed}
    tell theMessage
        make new to recipient at end of to recipients with properties {name:"John Doe", address:"johndoe@domain.com"}
    end tell
    tell content of theMessage
        make new attachment with properties {file name:theAttachment} at after last paragraph
    end tell
    send theMessage
end tell