Import mail attachments -- just the attachments

I am using a network scanner to scan and email me PDFs of a variety of documents. What I would like to do is to come up with a script to have devonthink grab those PDFs, which again are email attachments, import them, OCR them, and then move them to a folder.

Many thanks in advance for your thoughts on this!

You could write this as a mail rule applied to all messages coming in from your scanner.

  1. Make a script to include in a mail rule to save attachments to a folder (see sample, below)

  2. Make a folder action in Automator that takes new items in that folder, OCRs them, and saves them to DT in the destination configured in Preferences > Import > Destination

Script – here is an example (needs modification to specify the destination folder in Finder). Modify this for your local circumstances. Save wherever you want to save it, and create a mail rule that applies to a given account and/or FROM and/or TO, or whatever suits your usage.

using terms from application "Mail"
	on perform mail action with messages theMsges
                (* modify the next line to include the path to your attachment's folder *)
		set theAttachmentPath to (path to desktop) as text
		tell application "Mail"
			repeat with ThisMessage in theMsges
				set Attached to mail attachments of ThisMessage
				repeat with ThisAttach in Attached
					set FileName to name of ThisAttach
					save ThisAttach in theAttachmentPath & (FileName)
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from

using terms from application "Mail"
	on run
		tell application "Mail" to set sel to selection
		tell me to perform mail action with messages (sel)
	end run
end using terms from

Many thanks. I will give this a try.

Best, Edward Walpin