A single action to create email/markdown email?


#1

Drafts comes preloaded with two email actions. One sends the draft as a regular plain text email, and the other converts a markdown draft to html.

The only difference between the two is, in the markdown email action, the body tag is wrapped in double %%, and the “send as html” toggle is on.

I’d like to replace those two separate actions with a single email action that lets me choose “email” or “markdown email” after I launch it.

Since my goal is to reduce the number of actions altogether, I don’t want to create one action that runs another action. Can anybody suggest a script that will wrap the body tag in double %% and toggle “send as html” on, depending on which option I choose?

Long winded explanation, but I hope someone can help. Thanks so much.


#2

Here’s an example of one way to do what you are asking:

(() => {
    const 
        opts = ['Plain Text', 'HTML'],
        p = opts.reduce(
            (prompt, buttonName) => {
                prompt.addButton(buttonName);
                return prompt;
            },
            Object.assign(
                Prompt.create(), {
                    title: 'Mail',
                    message: 'Choose format to send mail',
                    isCancellable: true
                }
            )
        ),
        choice = p.show(),
        html = p.buttonPressed == 'HTML' ? true : false,
        body = html ? draft.processTemplate('%%[[body]]%%'): draft.processTemplate('[[body]]'),
        mail = Object.assign(Mail.create(), {
          subject: draft.title,
            body: body,
          isBodyHTML: html
        }),
        success = mail.send();
    if (!success) {
      console.log(mail.status);
      context.fail();
    }
})()

Here’s the action I posted.


#3

Brilliant! Exactly what I was looking for. Thanks so much.