This is the devonthink script
Mail Rule - File messages & attachments
– Created by Christian Grunenberg on Fri May 25 2012.
– Copyright (c) 2012-2020. All rights reserved.
– POSIX path of destination database. Global inbox is used if not specified.
property pDatabasePath : “”
– This string is used when the message subject is empty
property pNoSubjectString : “(no subject)”
using terms from application “Mail”
on perform mail action with messages theMessages for rule theRule
– Location of destination groups.
set pMessageLocation to “/” & (name of theRule)
set pAttachmentLocation to pMessageLocation & “/Attachments”
tell application id “DNtp”
if pDatabasePath is “” then
set destination_database to inbox
else
set destination_database to open database pDatabasePath – Ensure that the database is open
end if
set message_group to create location pMessageLocation in destination_database
set attachment_group to create location pAttachmentLocation in destination_database
end tell
tell application “Mail”
set theFolder to (POSIX path of (path to temporary items))
repeat with theMessage in theMessages
try
tell theMessage
set {theDateReceived, theDateSent, theSender, theSubject, theSource, theReadFlag} to {the date received, the date sent, the sender, subject, the source, the read status}
end tell
if theSubject is equal to “” then set theSubject to pNoSubjectString
tell application id “DNtp”
set theRecord to create record with {name:theSubject & “.eml”, type:unknown, creation date:theDateSent, modification date:theDateReceived, URL:theSender, source:(theSource as string), unread:(not theReadFlag)} in message_group
perform smart rule trigger import event record theRecord
end tell
repeat with theAttachment in mail attachments of theMessage
try
if downloaded of theAttachment then
set theFile to theFolder & (name of theAttachment)
tell theAttachment to save in theFile
tell application id “DNtp”
set theAttachmentRecord to import theFile to attachment_group
set unread of theAttachmentRecord to (not theReadFlag)
set URL of theAttachmentRecord to theSender
perform smart rule trigger import event record theAttachmentRecord
end tell
end if
end try
end repeat
end try
end repeat
end tell
end perform mail action with messages
end using terms from