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.

2 Answers 2

  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
  • File is not getting attached, it is only sending mail with plain text. Any help is much appreciated, thanks Sep 21, 2018 at 18:46

I noticed it started to work if the file reference is of type alias.

refer to a file as alias:

set desktopFolder to (path to desktop as alias) 
set exportFolderName to "Export"
tell application "Finder"
    --  get files from my Export folder
    set exportedFiles to every file of folder exportFolderName in desktopFolder
end tell

--get first file
set exportedFile to item 1 of exportedFiles
set exportedFileAlias to exportedFile as alias
set exportedFileName to exportedFile as text

and then it worked using

make new attachment with properties {file name:exportedFileAlias} 

but afterwards it also worked this way

make new attachment with properties {file name:exportedFileName} 

still confusing

and now it also works for POSIX path like "/Users/name/Desktop/Export/file.jpg"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .