Thank you very much for the thoughts - it turns out that there MAY be a slightly easier way, but, I’m not proficient enough in DT and AppleScript to get there.
I was looking at the included Bundles with MailMate and noticed that Eagle Filer has a bundle which does exactly what I want. I took advantage of their 30 day free trial to test, and without fail, the MM Bundle which extracts Tags from MailMate communicates via AppleScript to Eagle Filer and preserves those tags for archived mail.
From what I understand of the respective scripts, this seems like something that DevonThink can also do, but, the only missing piece for me is: How does AppleScript talk to DevonThink so that Tags are added to imported items?
The Eagle Filer AppleScript has two components. First is the Add.mmCommand:
{
name = 'Add';
input = 'raw';
environment = 'MM_SUBJECT=${subject.prefix:+${subject.prefix} }${subject.blob:+[${subject.blob}] }${subject.body:no subject}\nMM_MESSAGE_ID=${message-id.split}\nMM_TAGS=${##tags}\n';
command = '#!/bin/bash\n"${MM_BUNDLE_SUPPORT}/bin/add"';
keyEquivalent = "^A";
uuid = 'F2C8D509-4239-4EA0-9D85-AF44025A0982';
}
Which, as you can see, grabs the tags from MailMate directly.
That then calls the “add” command, which reads:
#!/bin/bash
name=${MM_SUBJECT//:/_}
name=${name//\//_}
name=${name:0:250}
mkdir -p "$TMPDIR/com.freron.MailMate.EagleFiler"
tmpfile="$TMPDIR/com.freron.MailMate.EagleFiler/${name}.eml"
cat > "${tmpfile}"
url="message://%3c${MM_MESSAGE_ID}%3e"
tags="${MM_TAGS}"
osascript "${MM_BUNDLE_SUPPORT}/bin/EagleFilerImport.scpt" "${tmpfile}" "${url}" "${tags}"
# unlink "${tmpfile}" # Handled by EagleFiler
Again, pretty straightforward, as it’s calling the AppleScript with the Tags as an argument.
That AppleScript reads:
on run _argv
-- Use separate handler to avoid creating implicit properties that would modify the script file when run.
set _tagsString to item 3 of _argv
set AppleScript's text item delimiters to " "
set _tags to _tagsString's text items
my importToEagleFiler(item 1 of _argv, item 2 of _argv, _tags)
end run
on importToEagleFiler(_emlPath, _sourceURL, _tags)
try
tell application "EagleFiler"
-- Wait for document to be re-opened if we are launching EagleFiler.
repeat 10 times
if not (exists document 1) then
delay 1
end if
end repeat
-- If lots of imports are queued, this may take a while. EagleFiler would finish the import,
-- but the script would report a timeout error, anyway. So make the script wait longer.
with timeout of 10 * 60 seconds
if _tags is {} then -- use default tags
import files {_emlPath} source URL _sourceURL with deleting afterwards
else
import files {_emlPath} source URL _sourceURL tag names _tags with deleting afterwards
end if
end timeout
end tell
on error _errorMessage number _errorNumber
tell application "System Events" -- Why not MailMate?
activate
display alert "EagleFiler: " & _errorNumber message _errorMessage
end tell
end try
end importToEagleFiler
And it works fine. So, it seems possible to me that if I understood AppleScript and DevonThink a bit more, I could modify the existing bundle to perform a similar task.
The current MailMate Bundle for DevonThink is pretty simple - it has the Add.mmCommand which sets up some variables (and is easy enough to modify to include the Tags), and then references the bin/add command, which embeds AppleScript within it:
#!/bin/bash
tmpfile="$TMPDIR/8D444CFE-C1F9-4F82-AD8B-4EA7EC7A77C2.eml"
cat > "${tmpfile}"
# Tricky substitution to make it work for " and \ in the AppleScript below.
name=${MM_SUBJECT//\\/\\\\}
name=${name//\"/\\\"}
url="message://%3c${MM_MESSAGE_ID}%3e"
osascript <<END
try
tell application id "DNtp"
set theRecord to import "${tmpfile}" from "MailMate" name "${name}"
set modification date of theRecord to creation date of theRecord
set the URL of theRecord to "${url}" # Allows it to be opened in MailMate using ⌃⌘O
display notification "Added message \"${name}\" to DEVONthink" with title "MailMate"
end tell
on error errMsg number eNum
tell application "System Events"
activate
display alert "DEVONThink: " & eNum message errMsg
end tell
end try
END
unlink "${tmpfile}"
So, if DevonThink can be told via AppleScript to set tags, it seems that I should be able to modify the osascript above to inherit the tags from the message, and set them for the imported item.
The closest I’ve come to writing AppleScript was HyperCard, decades ago, and that’s a completely different beast.
According to Script Editor and the DevonThink Dictionary, there should be a way to set tags on a record, but that’s about as far as I’ve got so far.
Any pointers from anyone would be appreciated!