Scriptd

A collection of scripts that I have written or found to be useful.

I do a fair amount of work in Markdown these days. I won’t get into the virtues of Markdown here. Chances are, if you have stumbled upon this page you’re already using Markdown and don’t need me to tell you why it’s a good idea.

I wanted a workflow that simplifies composing email in Markdown. Whether I am writing it in a text editor (generally BBEdit or Byword) or in my mail client itself, I wanted a quick way of converting Markdown to formatted text.

The main part of this workflow is a fairly simple shell script, which you’ll find below.

The script takes Markdown text on the pasteboard, converts it to rich text, and puts that rich text back on the pasteboard. This allows you to copy (or cut) a bunch of Markdown text, run the script, and paste it formatted into your email editor.

I have bound the script, using FastScripts, to ⌥⇧⌘D (Alt-Shift-Command-D).

This allows me to write a bunch of Markdown in Mail.app, select, cut, convert, and paste using the following three shortcuts: ⌘X (Cut) ⌥⇧⌘D (Convert) ⌘V (Paste)

Here’s the script, which I called Pasteboard Markdown to RTF:

#!/usr/bin/env bash

CSS_FILE="file:///Users/paul/Dropbox/Stylesheets/PasteboardMarkdown.css"

(echo "CSS: $CSS_FILE" ; echo ; pbpaste) | \
/usr/local/bin/mmd | \
textutil -stdin -stdout -convert rtf -format html | \
pbcopy

First, I define a variable called CSS_FILE that points to the CSS file I want to use to format the Markdown file. I took swiss.css from the Marked application bundle (found in /Applications/Marked.app/Contents/Resources/), copied it to my Dropbox folder, and renamed it to PasteboardMarkdown.css. You can substitute it with whatever CSS you prefer and place it wherever you like. Just make sure the CSS_FILE variable points to it using the proper file:/// URL syntax.

Next is a big Bash one-liner. The MultiMarkdown processor recognizes an input line that specifies the CSS file to be used. That’s what the first echo does. The second echo gives us a blank line (it doesn’t work otherwise). The pbpaste command pastes the contents of the pasteboard to stdout. The two echoes and the pbpaste combine the CSS specifier and the Markdown text from the clipboard.

All of that gets piped into the mmd MultiMarkdown processor, which takes Markdown text from stdin and writes HTML to stdout.

The HTML output is then piped into textutil which, using the options provided here, takes HTML from stdin and writes RTF to stdout.

Finally, the RTF output is piped into pbcopy which takes whatever is written to stdin (in this case, the RTF text) and puts it on the pasteboard.

Note, the script requires Fletcher Penny’s MultiMarkdown processor.

  1. scriptd posted this