Script to send selected records attached to e-mail

DT3 provides two ways to send records via e-mail

  1. with the context menu “Send E-Mail”
  2. with the smart rule action “Send E-Mail”

These two behave differently: Whereas the first actually sends the record(s?) attached to the e-mail, the second one sends the x-devonthink URL, aka “reference”. It is such pretty much useless outside of the current DT3 installation.

Therefore, I provide the following AppleScript (sic!) to use as an embedded script in smart rules. It has to be adapted before it can be used (recipient, subject and possibly body). The script sends every record as a single attachment in a separate e-mail. If you need one e-mail with all the records attached, you have to modify it slightly.

Also note that there’s a delay 1 in the code. DO NOT REMOVE it. This delay is due to the somewhat imperfect implementation of Apple’s Mail: without the delay, the mail is send before the file is attached to it. It’s a shame that they implement asynchronous operations without a proper mechanism to deal with them, but that’s Apple’s automation…

on performSmartRule(theRecords)
	set theReceiver to "" -- ENTER RECEIVER HERE
	set theBody to "" -- FILL IN if you need a message text
	set theSubject to "Subject" -- ENTER SUBJECT HERE
	tell application id "DNtp"
		repeat with theRecord in theRecords
			sendMail(path of theRecord, theBody, theSubject, theReceiver) of me
		end repeat
	end tell
end performSmartRule

on sendMail(theFile, theBody, theSubject, theReceiver)
	set AppleFile to POSIX file theFile
	tell application id ""
		set msg to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
		tell msg
			make new to recipient at end of to recipients with properties {address:theReceiver}
			make new attachment with properties {file name:AppleFile as alias}
			delay 1 -- DO NOT REMOVE!
		end tell
		send msg
	end tell
end sendMail

This is currently intended but a future release might add another action.

That’s brillant - thank you very much.

I’ve added the following lines above ```sendMail``

set theDialog to ("Send document to " & theReceiver & "?")
display dialog theDialog with title "Notice" buttons {"Cancel", "OK"} default button 2```

That way I get to see that a mail is to be sent, and can cancel if I so wish (e.g. if the SmartRule is badly defined and picks up documents not designed to be shared)

Thanks for this - I was looking exactly for a similar solution. Did you ever work on a script for multiple attachments?

No, but it would be simple to modify this script accordingly.