[MlMt] Creating a bundle template for message data, including attachments

P crp at f-m.fm
Fri Oct 1 12:13:34 EDT 2021


Hello everyone,

I've been exploring the world of MailMate bundles recently. They are a 
great format for anything to be shared, or for relatively simple 
scripts. However, as some of my scripts have become more and more 
complicated, I've started to find my proliferating .mmcommand and 
executable files a bit unmanageable. Debugging can be particularly 
tricky.

Given that the vast majority of my bundle commands would be of no use to 
anyone besides myself, I'm trying a different strategy: creating a 
simple script to pull out message information and send it to Keyboard 
Maestro in JSON format. Once in KM, it's easy to pass it to whatever 
script or app is needed. I also find it easier to keep everything 
organised this way.

Here's what the .mmcommand file looks like:

```
{
	name					=		'template';
	input					=		'canonical';
	environment		= 
'macro=E61C4928-CE11-460D-ADD9-2B379C1BBB5D\nmsgID=${message-id.split}\nsubjectPrefix=${subject.prefix}\nsubjectBody=${subject.body}\nfromName=${from.name}\nfromAddress=${from.address}\ntoName=${to.name}\ntoAddress=${to.address}\ncontentType=${content-type.type}';
	command			=		'#!/bin/bash\n"${MM_BUNDLE_SUPPORT}/bin/template"';
	keyEquivalent	=		'^[';
	uuid					=		'47d6808e-e6a7-41d9-9d92-d2948423be26';
}
```

And here's the executable:

```
#!/bin/bash

tmpFile="$TMPDIR/8D444CFE-C1F9-4F82-AD8B-4EA7EC7A77C2.eml"
cat > "${tmpFile}"

osascript <<END

tell application "Keyboard Maestro Engine"
	do script "${macro}" with parameter 
"{\"tmpFile\":\"${tmpFile}\",\"msgID\":\"${msgID}\",\"subjectPrefix\":\"${subjectPrefix}\",\"subjectBody\":\"${subjectBody}\",\"fromName\":\"${fromName}\",\"fromAddress\":\"${fromAddress}\",\"toName\":\"${toName}\",\"toAddress\":\"${toAddress}\"}"
end tell

END

unlink "${tmpfile}"
```

As you can see, I put the KM macro ID into the environment variables, 
which allows a single executable file to send the JSONified data to any 
number of macros. I just have to create a new .mmcommand file each time.

The variables passed:

tmpFile (full-text of the message)
msgID
subjectPrefix
subjectBody
fromName
fromAddress
toName
toAddress

It takes a bit of detective work to figure out what the available 
environment variables are but I think I've gotten most of them. The one 
major thing that I've failed to figure out is how to get attachments.

In the [bundle 
documentation](https://github.com/mailmate/mailmate_manual/wiki/Bundles#inputfilespattern), 
under "inputFilesPattern," it's mentioned that attachments are 
accessible via the `MM_FILES` environment variable. However, I've found 
only one example of this in use ([by David 
Slochower](https://github.com/slochower/bundles/tree/master/My%20Save.mmbundle)). 
It's written in Python—a language that, unfortunately, I don't know 
very well. Consequently, I'm struggling to see how it works or how it 
could be adapted.

So, long story short, I am wondering if anyone has any other examples of 
getting attachment info from `MM_FILES`, or some advice on how I could 
integrate it into my above-mentioned template.

I know that `MM_FILES` itself provides data in JSON format, which can be 
tricky to parse in AppleScript, but there are several ways around that. 
I just can't seem to get anything from that environment variable, which 
makes me think that I must be missing something.

Many thanks,

Philip.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freron.com/pipermail/mailmate/attachments/20211001/aa62a4b0/attachment.htm>


More information about the mailmate mailing list