Background
It’s rare that I have a Markdown-related thought for which Brett Terpstra hasn’t already made some elegant thing, but for once I think I’ve done it!
After buying just about every Markdown writing app there is, I’ve finally circled back to Sublime Text 3 with Terpstra’s Markdown Editing package and Marked 2.
Federico Vittici’s epic review of Editorial, a really impressive iOS text editor, prompted me to give it a try, and one particular workflow has been tickling my fancy ever since: compose your message in Markdown, then in two taps pipe the rendered HTML message to Mail.app. Wonderful!
Naturally, I wanted to be able to do this on the desktop as well. To my complete surprise, Mr. Terpstra had not yet graced us with a solution for this, and neither did the internet at large. Months later, I posed an iffy question on Stack Overflow: can I use Python to get Markdown from Sublime Text 3 rendered to an email client? User fanti swooped in almost immediately with an alarmingly thorough answer. So I had no choice but to pick up the quest.
Disclaimer
I’ve only ever hacked away at Python and AppleScript, and I am no authority on either one. I’m simply sharing my progress with the hope that you’ll learn something from my baby steps or just get your Markdown-to-email workflow started.
As usual, if your computer explodes or the universe collapses on itself because of anything here, that’s your problem. No warranty.
Overview
We’re building a Sublime Text 3 build system so that we can hit ⌘+B and ship our Markdown off to Sparrow1.
Step 1: Install markdown2 module, set up converter script.
We’re going to have Python convert Markdown into HTML, so we need to install Python’s markdown2 module (thanks again, Stack Overflow!):
Now, save a file called convert.py somewhere handy – I keep mine with other scripts in my Dropbox folder:
This is where all the magic happens, and it took me a few hours to get it working properly with lots of Googling and successive failures learning opportunities.
We’re reading the specified text file, using markdown2 to convert it to HTML, escaping the result, then creating and calling ActionScript that gets piped to Sparrow. Even for uninitiated, this probably makes sense.
If you’ve never used Python before, be careful copying and pasting these examples! Python loves – needs, even – tabs and not spaces, so be sure to keep tabs preserved exactly as they are.
Step 2: Create the new Sublime Text build system.
Go to Tools → Build System → New Build System and add your new build:
Save this file in Sublimes Packages/User folder with a name like MarkdownToEmail.sublime-build. This will make the build system available within Sublime Text, where after a restart you’ll find it under Tools → Build System → MarkdownToEmail.
Note that my Python path is /usr/bin/python – you can check yours by opening Terminal and running…
…and your full python path will be printed for either verification or a new round of confusion.
Step 3: Try it!
Now you’ve got everything in place to compose a message in Sublime Text. Set your new build system and hit ⌘+B to render HTML in a new Sparrow message window. Hopefully it works, and if not you’ll want to do some troubleshooting:
- Are your paths all correct?
- Can you run convert.py from the command line, outside of Sublime Text?
- Did my feeble convert.py choke on a character or encoding that I hadn’t planned for or tested?
Other Ideas
This could clearly be improved. Ideas I’ve had thus far…
- Populate the subject with the name of the file, or even use YAML to set a subject, recipients, etc.
- Add an option to automatically delete the original Markdown once it becomes an email message.
- Figure out how to get this working with Mail.app and clients that other people actually use.
Let me know if you have any more ideas, run into problems, or if you’ve got any tips or objections!
-
What if you don’t use Sparrow? Probably a good call since it’s just waiting around to die, but I finally learned the answer to this: check the AppleScript Dictionary. Open the AppleScript Editor application, then choose File and Open Dictionary. This is a perfect reference of every app on your machine that has an AppleScript API, along with documentation for each one. This is ultimately how I found Sparrow’s htmlContent property. See if your client has some kind of support for passing in message text in HTML, and if so you can edit convert.py to suit. ↩