Adding Attachment To Item 1 In Drafts Mailbox

I have a script that uses a template to create a new draft that uses GUI scripting to add the recipent, the cc, and the subject. Now I want to add an attachment at the end of the document. My reference to the draft document does not seem to allow this. I can use the reference below to get the subject, the content, or the recipient, but I cannot set any of these with the reference. Any help would be greatly appreciated.

Here is where I left off:

set garagePath to "Macintosh HD:Users:mylogin:Documents:Garage Deed Restriction.pdf" as string

tell application "Mail"
	set theMessages to messages of drafts mailbox
	set theMsg to item 1 of theMessages
	tell theMsg
		tell content to make new attachhment with properties {file name:garagePath} at after last paragraph
	end tell
end tell

Browser: Firefox 73.0
Operating System: macOS 10.14

Hi. I would advise against GUI scripting, where possible. Try this:

tell application "Mail"
	tell (make outgoing message with properties {subject:"test", content:"message text", visible:true})
		make to recipient with properties {address:"address@domain.com"}
		make attachment with properties {file name:"Macintosh HD:Users:mylogin:Documents:Garage Deed Restriction.pdf"} 
	end tell
end tell

Thank you Marc for your response.

The message is made by selecting a template and keystroke “d” using {command down, shift down}.

This creates a new message in the drafts mailbox that opens on the screen.

The template has graphics and lots of text so I assume I must use it as a starting point.

How can I reference this draft new message so I can add attachments?

I have checked the Mail dictionary and can’t figure it out. It has no “send again” reference in the dictionary which is the “d” command down, shift down call.

I can get a reference to the draft using “set theMessage to first message of drafts mailbox”. It allows reads, but no writes.

Browser: Firefox 73.0
Operating System: macOS 10.14

Your workaround is wrong. You should not keystroke “d” and so on… Because to have the reference to outgoing message, you should open the outgoing message by the script, and not giving full control to the Mail.app.

Run this script:


set garagePath to alias "Macintosh HD:Users:mylogin:Documents:Garage Deed Restriction.pdf"

tell application "Mail"
	set aTemplate to message 1 of drafts mailbox
	tell (make outgoing message with properties {subject:(subject of aTemplate), content:(content of aTemplate), visible:true})
		make to recipient with properties {address:"address@domain.com"}
		make attachment with properties {file name: garagePath} at after the last paragraph
		send
	end tell
end tell

Replace address@domain.com with your email address to see the resulting message in your INBOX.

Thank you KniazidisR!

The graphics in the template are not carried over to the new message using your method. However, the way you approached your creation of the e-mail is something that I will be adopting in the future.

Again, thank you!

  1. Unfortunately, getting the content property as rich text does not work perfectly in Mail.app. But let’s hope that Apple will work on this and will soon fix the bug. In any case, the process of copying template’s graphics and text will remain the same (as I showed) in the future.

  2. In the Viewing tab of the Mail application Preferences, you can uncheck the “Use dark backgrounds for messages” checkbox. Then the color layout of the resulting message will become as close as possible to the original.

  3. GUI scripting (with copy/paste operations) may solve the issue 1) completely for now.