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)