<div class="markdown">
<p dir="auto">On 18 Apr 2014, at 18:28, Brett Terpstra wrote:</p>
<blockquote>
<p dir="auto">On 18 Apr 2014, at 9:41, Benny Kjær Nielsen wrote:</p>
<blockquote>
<p dir="auto">[…]<br>
I guess that makes part of commands semi-documented. You might want to ask about <code>output</code> types as well ;-)</p>
</blockquote>
<p dir="auto">Consider it asked.</p>
</blockquote>
<p dir="auto">Ok. As already indicated, <code>html</code> and <code>canonical</code> are going to be output types for filtering commands, but this is not yet functional for bundles. The default output type is <code>discard</code> and this leaves us with the only interesting output for now: <code>actions</code>.</p>
<p dir="auto">The <code>actions</code> output type expects a property list to be returned from the command. Here is a simple example:</p>
<pre><code>{
actions = (
{
type = "moveMessage";
mailbox = "archive";
}
);
}
</code></pre>
<p dir="auto">Each action must have a type. Additional keys may be allowed/required depending on the type. The currently available types are:</p>
<ul>
<li><p dir="auto"><code>playSound</code></p>
<p dir="auto"><code>path</code>: Full path or a sound name if the sound can be found in a standard sound path.</p></li>
<li><p dir="auto"><code>notify</code></p>
<p dir="auto"><code>formatString</code>: A format string (default is <code>"“${subject}” from “${from.name:${from.address}}”"</code>).<br>
<code>mailbox</code>: Mailbox identifier (click on a mailbox and do ⌘C to get this value).</p></li>
<li><p dir="auto"><code>moveMessage</code></p>
<p dir="auto"><code>mailbox</code>: Mailbox identifier (must be an IMAP mailbox).</p></li>
<li><p dir="auto"><code>copyMessage</code></p>
<p dir="auto"><code>mailbox</code>: Mailbox identifier (must be an IMAP mailbox).<br>
<code>variables</code>: More about this further below.</p></li>
<li><p dir="auto"><code>changeFlags</code></p>
<p dir="auto"><code>enable</code>: Array of IMAP flags/keywords, e.g., <code>( "\\Flagged", "\\Send")</code>.<br>
<code>disable</code>: Array of IMAP flags/keywords.</p></li>
<li><p dir="auto"><code>exportMessage</code></p>
<p dir="auto"><code>folderPath</code>: Simple disk path (it can also be a <code>file:</code> URL).</p></li>
<li><p dir="auto"><code>redirectMessage</code></p>
<p dir="auto"><code>recipient</code>: Redirect message to the recipient (this includes sending the message).</p></li>
<li><p dir="auto"><code>createMessage</code></p>
<p dir="auto"><code>headers</code>: Dictionary with headers for the message.<br>
<code>body</code>: Entire message body.</p></li>
<li><p dir="auto"><code>replyMessage</code> (currently always “Reply All”)</p>
<p dir="auto"><code>headers</code>: Dictionary with headers for the message.<br>
<code>body</code>: Reply part of message body.</p></li>
<li><p dir="auto"><code>runScript</code></p>
<p dir="auto"><code>scriptUUID</code>: The UUID of a bundle command. Note that this script can return actions itself.</p></li>
</ul>
<p dir="auto">Note that commands also support an <code>executionMode</code> which can be <code>singleMessage</code> or <code>multipleMessages</code> (default is <code>singleMessage</code>). This determines whether the script should be executed once for each message or once for all selected messages. (In <code>singleMessage</code> mode MailMate tries to handle any resulting actions efficiently by merging them if they are identical for subsets of messages. This is important for large message selections.)</p>
<p dir="auto">All actions allow an <code>ids</code> key which is an array of internal message ids (integers). If needed, these can be provided to a script using the virtual header named <code>#body-part-id</code>. This is only used internally by MailMate now, but it might be useful for external purposes which I have not realized yet.</p>
<p dir="auto">The <code>copyMessage</code> action is special since it has two different behaviors. If <code>variables</code> are <em>not</em> defined then it's a simple copy action equivalent to ⌥-dragging a message. If <code>variables</code> are defined then all headers and the body of the copied message are interpreted as being format strings for which the <code>variables</code> should be used. This can be used to create a draft message in MailMate with the purpose of using it as a template for an external script. The external script could, for example, handle a list of recipients for the draft message. An example is probably needed to understand how this works. Imagine creating a draft with values like this:</p>
<pre><code>To: ${to}
Subject: A personal message to you.
Hi ${firstname},
I wanted to tell you about an extraordinary email client named MailMate. I used it to create this very personal message.
Regards, Benny
</code></pre>
<p dir="auto">The <code>actions</code> could then be generated by a script with output like this:</p>
<pre><code>{ actions = (
{
type = copyMessage;
variables = {
to = 'Foo Bar <foobar@example.com>';
firstname = 'Foo';
};
},
{
type = copyMessage;
...
}
);
}
</code></pre>
<p dir="auto">A practical example is the emails I sent to existing license owners when doing the crowd funding campaign. Those emails were create by letting a Ruby script generate the actions. It also used the variables to include the existing license key of each user to make sure they did not have to search for it if they were no longer actively using MailMate. I could create the draft in MailMate using any feature of MailMate I'd like (Markdown, Send Later, ...). The script generated a huge number of emails in my drafts folder, but I could then review the result and add (really) personal messages to some of them. Furthermore, it made it easy to send out the emails in smaller batches. (This wouldn't work well for 100.000 emails, but in my case it was fine.)</p>
<p dir="auto">Caveat: When I used some of the message-generating features myself (and no-one else has I believe) I had some crashes which I'm not sure have been fixed yet. Reports are naturally welcome.</p>
<blockquote>
<p dir="auto">What I want to do is run my own custom html2text on the output and save it to a text file (for nvALT import purposes). The output I was getting from canonical was already "markdownified" in most cases, and it seemed that with html, there were cases where it would send nothing at all (assumed it was because there was no html section). If decoded provides the Content-Type boundaries, I can parse that...</p>
</blockquote>
<p dir="auto">No, <code>decoded</code> does not provide <code>Content-Type</code> directly, but you could use environment variables to do that. For example,</p>
<pre><code>environment = 'MM_CONTENT_TYPE=${content-type.type:text}\nMM_CONTENT_SUBTYPE=${content-type.subtype:plain}\n';
</code></pre>
<p dir="auto">But as noted, the real problem is probably to tell MailMate which body part to provide to the script.</p>
<p dir="auto">With respect to <code>canonical</code> not being the desired data (or the Markdown conversion being inadequate) then I'd like to fix that by extending the set of input types or by improving the HTML to Markdown conversion (maybe we should discuss your version of <code>html2text</code> off list).</p>
<p dir="auto">On a more general note, my goal is to provide input which takes care of as many of the email intricacies as possible before handing over data to commands or other parts of the interface. The many(!) problems concerning the conversion of headers and bodies to any kind of canonical data should be handled by MailMate to keep everything else as simple as possible. In other words, canonicalization should be my side of the fence.</p>
<blockquote>
<blockquote>
<blockquote>
<p dir="auto">Also, how does MM_SELECTED_RANGE work when the input to the command isn't the same format as what was selected in the viewer?</p>
</blockquote>
<p dir="auto">In a sense it's never the same format since even a plain text message is displayed as HTML. To provide <code>MM_SELECTED_RANGE</code>, MailMate heuristically re-locates the selected text in the canonical text when a command is executed. This currently does not happen for the <code>html</code> input type.</p>
</blockquote>
<p dir="auto">Any chance that when there's a selection and the current view type is HTML, it could send the raw selected HTML to the output? As in, MM_SELECTED_TEXT instead of just a range?</p>
</blockquote>
<p dir="auto">To be consistent I think it should be a new input type: <code>html_selection</code>. And for the <code>html</code> input type <code>MM_SELECTED_RANGE</code> should be made available as it is for <code>canonical</code> (I'm not sure how easy the latter would be).</p>
<p dir="auto">-- <br>
Benny</p>
</div>