Attaching files to an existing Mail.app message

Anyone have a guess why the following script is not successfully attaching a file to an existing Mail.app messge? It’s part of a Filemaker routine I’m creating to export a number of binary and graphics files to a folder and then attach the contents to a mail message created using Filemaker’s scripting.

Here’s the routine:

  1. Export selected files (residing in a container field) to a folder on the desktop. (that works fine)
  2. Use the Send Mail script step in Filemaker to create a new message (also working fine)
  3. Attach the specified files (residing in a folder on the desktop) to the email message just created by Filemaker. (not working at all).

I’ve simplified the problem by only attempting to attach a single file with a known name and location. I keep getting the following error:

Mail got an error: NSArgumentsWrongScriptError

Here’s the Applescript:


set theAttachment to "Macintosh HD:Users:douggardner:Desktop:FilemakerMailTest:Attachment2.pdf" as alias
tell application "Mail"
	activate
	tell content of outgoing message 1
		make new attachment with properties {file name:theAttachment}
	end tell
end tell

zero:

Try this syntax:

make new attachment with properties {file name:WHATEVER} at after the last paragraph

Thanks, but I get the same error message when I try:


set theAttachment to "Macintosh HD:Users:douggardner:Desktop:FilemakerMailTest:Attachment2.pdf" as alias
tell application "Mail"
	activate
	tell content of outgoing message 1
		make new attachment with properties {file name:theAttachment} at after the last paragraph
	end tell
end tell

Any other ideas?

Sure I do. Here is the complete code (the names have been changed to protect the innocent) of what I use:

tell application "Mail"
	set b to make new outgoing message with properties {sender:"d@d.com", subject:("Please work")}
	tell b to make new to recipient
	set content of b to "Hello:

Hope this works, " & (short date string of (current date)) & return & return & "dd" & return & return
	tell b's content
		make new attachment with properties {file name:WHATEVER as alias} at after the last paragraph
	end tell
	set address of first to recipient of b to "e@e.net"
	send b
end tell

The only real difference I can see is that I set the WHATEVER file name to an alias within the Mail tell block, while you set yours without. It could make a difference, so give it a try.

Thanks for the help, CAS. Unfortunately, my problem isn’t creating a new message. I use this code and it works fine:


set theName to "SomeOne"
set theAddress to "someone@somewhere.com"
set theSubject to "Mail Subject"
set theBody to "Body Text"
set theAttachment to "Macintosh HD:Users:douggardner:Desktop:FilemakerMailTest:Attachment2.pdf" as alias
tell application "Mail"
	set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
	tell newMessage
		set visible to true
		make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
		tell content
			make new attachment with properties {file name:theAttachment} at after the last paragraph
		end tell
	end tell
	activate
end tell

The problem is that I can’t add an attachment to an existing mail message. I’ve tried it with the path to the attached file in a variable and without a variable–it really doesn’t make a difference. Thanks for your help, though. If anyone can solve this problem, there are a lot of people out there that would be greatful (judging by the 300+ posts I waded through today).

Doug

Doug:

I am sorry, that is not what I meant. I meant to try setting the variable for the location of the attachment to an alias within the Mail tell block, like this:


set theName to "SomeOne"
set theAddress to "someone@somewhere.com"
set theSubject to "Mail Subject"
set theBody to "Body Text"
set theAttachment to "Macintosh HD:Users:douggardner:Desktop:FilemakerMailTest:Attachment2.pdf"--Don't use the [as alias] here
tell application "Mail"
	set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
	tell newMessage
		set visible to true
		make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
		tell content
			make new attachment with properties {file name:theAttachment as alias} at after the last paragraph --use it here instead
		end tell
	end tell
	activate
end tell

That should work, as long as the path defined in the set theAttachment line is actually there. If it does not work, try specifically telling the content of the message, like this:


set theName to "SomeOne"
set theAddress to "someone@somewhere.com"
set theSubject to "Mail Subject"
set theBody to "Body Text"
set theAttachment to "Macintosh HD:Users:douggardner:Desktop:FilemakerMailTest:Attachment2.pdf"
tell application "Mail"
	set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
	tell newMessage
		set visible to true
		make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
		
	end tell
	tell newMessage's content
		make new attachment with properties {file name:theAttachment as alias} at after the last paragraph
	end tell
	activate
end tell

Thanks again, Craig.

Everything you sent works fine. Still, I am having trouble adding an attachment to an existing email message (everything works if I have the Applescript create a new message). Here’s the code I’m using:


set theAttachment to "Macintosh HD:Users:douggardner:Desktop:FilemakerMailTest:Attachment2.pdf"
tell application "Mail"
	activate
	tell content of outgoing message 1
		make new attachment with properties {file name:theAttachment as alias} at after the last paragraph
	end tell
end tell

I think the problem is in the “tell content of outgoing message 1” line. I think it may have to do with specifying the outgoing message properly. The error message I get says:

Mail got an error: NSContainerSpecifierError

Thanks again,

Doug

Doug:

More apologies. I did not notice that you were interested in editing an already created message instead of script-created message. That is truly the holy grail of scripting Mail, and as you can see with a search of this board (and others), it remains a mystery. I am certainly no master scripter, but I do believe that the current version of Mail is unable to perform such a task.