I use the following script in Apple Mail attached to a rule. For different reasons I want to deactivate my rules in Apple Mail and instead run this script manually , from external (Keyboard Maestro).
To you helpful souls here: What would I have change in the code to make this happen?
-- Automatically file messages & attachments
-- Created by Christian Grunenberg on Fri May 25 2012.
-- Copyright (c) 2012. All rights reserved.
-- POSIX path of destination database
property pDatabasePath : "/Users/me/Documents/DEVONthinkPro/datenbanken/Studio_mail.dtBase2"
-- 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 pEmailLocation to "/Studio/Active"
set pMessageLocation to pEmailLocation
set pAttachmentLocation to "/Attachments_Studio"
tell application id "com.devon-technologies.thinkpro2"
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 email_group to create location pEmailLocation in destination_database
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 "com.devon-technologies.thinkpro2"
set theRecord to create record with {name:theSubject & ".eml", type:unknown, creation date:theDateSent, modification date:theDateReceived, URL:theSender, source:(theSource as string)} in message_group
set unread of theRecord to (not theReadFlag)
end tell
repeat with theAttachment in mail attachments of theMessage
set theFile to theFolder & (name of theAttachment)
tell theAttachment to save in theFile
tell application id "com.devon-technologies.thinkpro2"
set theAttachmentRecord to import theFile to attachment_group
set unread of theAttachmentRecord to (not theReadFlag)
end tell
end repeat
move theMessage to mailbox named "DEVONthink"
end try
end repeat
end tell
end perform mail action with messages
end using terms from
Thanks for listening!
Bernd