Extract email attachments with original file metadata and linking

Hi, I am new to DEVONthink, so please forgive me if the answers are obvious.

I am struggling with the email import which is really important for my workflow. I know from looking through this forum that the Apple Mail scripting no longer works since Sonoma. I am using the Import feature in DEVONthink which still works for Apple Mail and I find the archiving functionality outstanding. There is only 2 big issues for me:

(1) I often need the attachments within the emails to be extracted as separate documents and they need to retain their original file metadata. However, when I manually drag and drop them from the email, the resulting documents in DEVONthink get a created date from the moment when I dragged and dropped them. It would neither be helpful if the attachments got the date from the original email (like some scripts in this forum do). I just need the correct original metadata from the email attachments (like the correct creation date of an attached contract). How do I solve this?

(2) Upon extraction of the attachment(s), the extracted files should be automatically linked to the original email they were exported from to keep the audit trail. This is also a general issue I am facing: How do I easily link two or several documents/files between each other without having to copy and paste several manual links in each document/file?

Thank you very much for your help!
Kind regards

Here’s the applescript I use to process specific email messages

Click to see script

This text will be hidden

set MailScript to (load script "/Users/DTLow/Library/Scripts/Applications/Mail/Add message(s) & attachments to DEVONthink.scpt")

tell application id "DNtp" to set theGroup to preferred import destination

tell application "Mail"
	
	set theFolder to (POSIX path of (path to temporary items))
	set TheMessages to (messages of mailbox "Devonthink" of account "iCloud")
	repeat with theMessage in TheMessages
		MailScript's imPortMessage(theMessage, theFolder, theGroup)
		delete theMessage
	end repeat
	
end tell

It makes use of the Add message(s) & attachments to DEVONthink
script supplied by Devonthink,
and creates a group for the email and attachments in the Global Inbox

I use a group for the original email and attachments

PDF files retain some metadata internally
This can be viewed using Preview/Inspector
For example
image

That’s a bit vague. There is metadata that is part of the file (like the Producer of a PDF) – that will, of course, be preserved. And there is metadata, like Finder comments, creation date, keywords, that exists separately from the file proper and lives only in the file system, not the file itself. Those will not be transmitted with the file attachment and consequently can’t be saved.

You don’t. The creation date is not part of the file, it’s a property of the file system. You get sent a file, not its creation date. That’s a feature/short coming of MIME etc. Your best bet is to receive PDFs which contain the creation date in their metadata, i.e. inside the file. You can then extract it with a script and use it to set DT’s creation date.

Thank you very much.

I have tried to run your applescript. The group gets created and the original email is imported into DEVONthink, but the attachment is not imported as an individual document/file into this group. I suppose this is because since Sonoma, Apple Mail scripts do not work as expected anymore.

Thank you also for your explanations on PDF metadata.

Thank you very much, chrillek. Indeed, I was not aware that the file creation date is always file-system specific. I thought there is some file related metadata (not the date the file was added by downloading it but the original creation date) which remains unchanged even though it gets moved to a different file system. Thank you for this insight. So indeed it would make sense to manually set the creation date to the date of the original email.

Welcome @Thorsten

Does DEVONthink have Full Disk Access and its Automation requests allowed in System Settings > Privacy & Security? If not, they should.

And there is an Apple Mail script in the global scripts menu available when in Apple Mail. Select an email with an attachment, then run the script from the scripts menu.

1 Like

Thank you very much, bluefrog. This was the missing piece! Now everything works smoothly.

You’re welcome and we’re glad to hear it’s working now!