Applescript for attaching files to Mac Mail.. help.

Hi all, I’m working on an applescript to work as the end part of a Filemaker Pro script. It runs at the end by running the below script to allow a user of a FMpro database to add multiple attachments (that have just been exported) to an email with Mac Mail. I have got as far as the below. The new email is generated and the dialog window comes up to add the attachments. Unfortunately when they are selected and you press choose. They don’t attach. Any ideas? ¨
¨
tell application “Mail”
set frontWindow to window
set newMessage to make new outgoing message
tell newMessage
set theAttachment to choose file with multiple selections allowed
end tell
end tell

Hi,

first of all, welcome to MacScripter.
Second of all, please post questions which don’t involve Xcode (Apple’s developer environment) in the AppleScript | OS X section.
Third of all, please use the AppleScript tags for code

An attachment in Mail is an object, so you have to create one attachment object for each file in the content of the message


set theAttachments to choose file with multiple selections allowed
tell application "Mail"
	set newMessage to make new outgoing message
	repeat with anAttachment in theAttachments
		tell newMessage
			tell content to make new attachment with properties {file name:anAttachment} at after the last paragraph
		end tell
	end repeat
end tell

Great, thank you! Do you know if there is a way to make the new email itself (the window) come to the front (as it is appearing behind Filemaker Pro ?) Also, is there a way to add spacing between each attachment in the body of the email?

Add a line to activate Mail and to make the message window visible


set theAttachments to choose file with multiple selections allowed
tell application "Mail"
	activate
	set newMessage to make new outgoing message with properties {visible:true}
.

Regarding the spacing I have no idea, but as the content property of a message is plain text, it might be possible to add characters

Thanks so much. I would for example want to add the filenames of the attachments between each one… tricky…

actually the filenames are displayed anyway in the message

thanks, they are not in words…? it just adds the images as attachments (in this case jpegs).

kind regards

try this


set attachmentFiles to choose file with multiple selections allowed
set theContent to ""
set attachmentCount to (count attachmentFiles) * 3
repeat with aFile in attachmentFiles
	tell application "System Events" to set fileName to name of aFile
	set theContent to theContent & fileName & return & return & return
end repeat

tell application "Mail"
	activate
	set newMessage to make new outgoing message with properties {content:theContent, visible:true}
	repeat with anAttachment in attachmentFiles
		tell newMessage
			tell content to make new attachment with properties {file name:anAttachment} at after paragraph (-attachmentCount + 1)
			set attachmentCount to attachmentCount - 3
		end tell
	end repeat
end tell

amazing. thank you

Sorry to be fussy, just wondering how to make the text appear below the images. Also, would there be a way to leave out the extension part (.jpeg).

Thanks so much.

here we go


set attachmentFiles to choose file with multiple selections allowed
set theContent to ""
set attachmentCount to (count attachmentFiles) * 3
repeat with aFile in attachmentFiles
	tell application "System Events" to set {fileName, fileExtension} to {name, name extension} of aFile
	set baseName to text 1 thru ((get offset of "." & fileExtension in fileName) - 1) of fileName
	set theContent to theContent & return & return & return & baseName
end repeat

tell application "Mail"
	activate
	set newMessage to make new outgoing message with properties {content:theContent, visible:true}
	repeat with anAttachment in attachmentFiles
		tell newMessage
			tell content to make new attachment with properties {file name:anAttachment} at after paragraph -attachmentCount
			set attachmentCount to attachmentCount - 3
		end tell
	end repeat
end tell

Fantastic, thanks so much!! One (hopefully last) question. In making the new email any signature as set up within mac mail preferences get erased. Would there be a way to keep that in?

try to insert these lines before the last end tell line and replace mySignature with the proper signature name


delay 1
set message signature of newMessage to signature "mySignature"

Thanks for your help!

hi there, this might be a bit outside the box but I wondered if there was a way to step the applescript to automatically create a new email and automatically add attachments to it that were made / created on the desktop in the last say 5-10 seconds?

That would skip the step of the user having to add the attachments.

This would always work because this applescript runs a second after the user have exported their chosen images from the database so the desktop (always to the desktop).

thank you!

Basically it’s possible with a folder action.
A folder action is attached to a specific folder and the associated script is triggered whenever a file is added to the folder.

The generic folder action code for adding items


on adding folder items to this_folder after receiving attachmentFiles
	.
end adding folder items to

wraps the whole script and replaces the choose file line

Thanks, so in my case I would need to add the first part at the beginning of my script and the last part at the end?

How do you make it state “desktop” and how does it know to grab any recent images there, could be 2 could be 10 etc?

Sorry, pretty new to all this…

thank you!

Yes, exactly

please read the folder action documentation how to attach an action to a folder and a script to the action.
Folder Actions Reference

However you might run into a problem when the whole set of images is not added at the same time.
For example 3 files have been exported from somewhere and are added to the folder one per second, in this case the folder action is triggered three times, which is not what you want.
Now it gets complicated because you need something like a timeout logic “create the message after 10 seconds of inactivity”. This is quite difficult to accomplish in conjunction with a folder action

I’m reviving this topic because I cannot get something to work. My script looks like this:

tell application "Finder"
	set myFolder to (path to desktop as text) & "From FileMaker"
	set attachmentFiles to get every item of (entire contents of folder myFolder)
	
	set mySubject to "This is my subject"
	
	set theContent to "Hi all" & return & return & "Blablabla" & return & return & "Best regards," & return & "Me"
	
	set attachmentCount to (count attachmentFiles) * 3
	repeat with aFile in attachmentFiles
		set theContent to theContent & return & return & return
	end repeat
	
	tell application "Mail"
		activate
		set newMessage to make new outgoing message with properties {subject:mySubject, content:theContent, visible:true}
		tell newMessage
			make new to recipient with properties {address:"blabla@blabla.com"}
		end tell
		
		repeat with anAttachment in attachmentFiles
			tell newMessage
				set anAttachment to anAttachment as alias
				tell content to make new attachment with properties {file name:anAttachment} at after paragraph (-attachmentCount + 1)
				set attachmentCount to attachmentCount - 3
			end tell
		end repeat
	end tell
end tell

It works well for more than 1 attachment. But if there is just 1 attachment, the attachment isn’t added to the mail. It would be great if someone more knowledgable could take a look because I’ve been trying to understand what’s going on for the last hour or so, and I didn’t succeed.

Never mind. I figured it out.

To get the files, I now use:

set attachmentFiles to get files of folder myFolder

And in the repeat loop I now use “as alias”:

			repeat with anAttachment in attachmentFiles
				tell newMessage
					set anAttachment to anAttachment as alias
					tell content to make new attachment with properties {file name:anAttachment} at after paragraph (-attachmentCount + 1)
					set attachmentCount to attachmentCount - 3
				end tell
			end repeat

Hope this helps some later reader.