Attach files to existing (outgoing) Mail Message

Dear Applescript Gurus,

I’m trying to write an AppleScript that will attach a given file to the end of an existing outgoing email using Apple Mail. I borrowed some code from an Apple example in /Library/Scripts that creates a new message and tried to modify it to work with an existing message, but I’m not having much luck. The end goal is to actually use this as an Alfred action, but I think I know how to finish that part once I get a working script. I’ve seen a few places that suggest Mail is not highly compatible with AppleScript, but I suspect there’s a workaround, especially since there is an Automator action called “Add Attachments to Front Message.” In fact, I suspect there’s an easy way to do this with automator, but I couldn’t figure out the method Alfred uses to pass parameters to Workflows. Anyway, the Alfred stuff is probably out of scope for this venue, but I throw it out there incase someone knows.

Here is the script I’ve come up with:


-- First open an outgoing message
tell application "Mail"
	set theAttachment to choose file

	set themessage to front outgoing message
	tell themessage
		tell content
			make new attachment with properties {file name:theAttachment} at after the last paragraph
		end tell
	end tell
end tell

As is I get the following error: error “Mail got an error: Can’t get content of outgoing message id 63.” number -1728 from content of outgoing message id 63

Based on testing, I’m pretty sure that “id 63” is referring to the newly opened message, since it increments every time I create a new message. I’ve also tried setting “visible” and other atributes, but it doesn’t like it.

Thanks in advance!

Model: Macbook Pro (Early 2011)
AppleScript: 2.2.1
Browser: Safari 535.17
Operating System: Mac OS X (10.7)

This this this!

I have found tons of people with the alias problem. But I can’t find why it is that I can’t add an attachment to a new email.

Did you ever have any result with this?

Nope, I never could get it to work. I’m pretty sure it’s a bug. Let me know if you figure anything out!

Ok, I managed to get it working.

Presenting how to paste an image from an existing image file into the body of an outgoing email. May internet history remember me fondly.


tell application "Mail"
	activate
	tell front outgoing message

		--SET CLIPBOARD TO IMAGE REQUIRED
		--IMAGE DEPENDS ON JIRA ISSUE TYPE
		if (jiraType = "Bug") then
			set the clipboard to (read (("Macintosh HD:Users:me:Dropbox:Applescripts:Mail - Add Jira:bug.gif") as alias) as TIFF picture)
		else if (jiraType = "Story") then
			set the clipboard to (read (("Macintosh HD:Users:me:Dropbox:Applescripts:Mail - Add Jira:requirement.gif") as alias) as TIFF picture)		
		else if (jiraType = "Task") then
			set the clipboard to (read (("Macintosh HD:Users:me:Dropbox:Applescripts:Mail - Add Jira:task.gif") as alias) as TIFF picture)		
		end if

		--PASTE INTO OUTGOING MESSAGE
		tell application "System Events" to keystroke "v" using command down
		
	end tell
end tell