Markdown compose for Mac OS X Mail

I have recently been delving into the world of Markdown, an easy lightweight markup syntax, mostly geared for writing for the Web. One popular Markdown app for iOS is Markdown Mail. I wondered how I could hack together a way to compose an email in Markdown and have it turned into HTML to send.

I started to make an OS X text service with Automator. The idea is that you select some text, then go to the services menu under the application’s menu or on the context (right-click) menu of the text. You select your service, it runs and usually is set to replace the selected text with its output.

The problem I encountered was that you can’t replace the text selection object — formatting and all — with the result of such a service. The plain text of the selection is sent to the service workflow, and plaintext is extracted from the result and that replaces the text characters of the selection, but not their format. I needed to be able to replace the selection with an HTML clipping rather than the HTML text that the Markdown converter generates. This is fine if you are in a text editor and are going to compose an HTML post or file by writing in Markdown, then selecting all and converting it to HTML text. You can see how to make this service in this nice post by Matt Gibson.

There might be some other way, but for now what I had to do was to save the HTML generated from the Markdown conversion to a temporary file, then open that in Safari, have Safari select all and copy to the clipboard (copying an HTML object), then having Mail paste it – replacing the selected Markdown text. So, the service workflow takes the selected text, but does not itself replace the selection directly.

To make this Automator service:

  1. Open Automator

  2. Choose to create a Service

  3. Set the initial options:

    • Service receives selected text in Mail
    • Uncheck “Replaces selected text” (we will be pasting in rich text ourselves)
  4. From the Actions library on the left, select “Automator” to see its actions.

  5. Select the “Run Shell Script” action and drag it to the workflow area or just double-click it.

  6. Replace the sample code with:

    /usr/local/bin/pandoc --smart -s -o /tmp/tmp.rtf
    open -a TextEdit /tmp/tmp.rtf

    [Update]: I originally used MultiMarkdown in this example and opened the resulting HTML in Safari, but I've had Safari give problems opening a file: URL. The solution I've come to is to use pandoc to convert the Markdown to RTF and open that in TextEdit. You could still use Safari if it works for you, just replace "TextEdit" with "Safari" in the Applescript below and use this code in the shell script:

    /usr/local/bin/pandoc --smart -s -o /tmp/tmp.html
    open -a Safari /tmp/tmp.html

    You can add other options for pandoc or use your favorite Markdown converter to produce an HTML output file.

  7. From the Automator actions list on the left, add a “Run Applescript” step to the workflow.

  8. In the script box, place this Applescript code:

    on run {input, parameters}

        tell application "TextEdit"        

            activate

            tell application "System Events"

                keystroke "a" using {command down}

                keystroke "c" using {command down}

                keystroke "w" using {command down}

            end tell        

        end tell

        tell application "Mail"        

            activate

            tell application "System Events" to keystroke "v" using {command down}

        end tell    

        return input

    end run

  9. Save the workflow as, for example, “Markdown to HTML Mail”. You could also save this as a regular workflow to the Mail application scripts folder (~/Library/Scripts/Applications/Mail) if you have the menubar scripts menu, then it would appear there when Mail is active.

Now, compose a new message in Mail, typing in Markdown. Then select all and either right-click on the text and go to the Services sub-menu or go to the Mail application menu and the Services sub-menu from there. You should find “Markdown to HTML Mail” there, and after selecting it, and a flash of action, your Markdown text has been replaced with rendered HTML. You can always use undo if it didn’t come out well, so you might want to use a variation of this service to have a Markdown preview to check on things until you are finished. That’s in the next post.