Tidying Markdown Tables
I’d been a fan of Dr Drang’s table tidy script for Markdown, especially the one he’s recently updated here.
However, he’s a BBEdit user and I don’t use BBEdit. I’m a fairly basic user, and I’ll stick with Textastic, nValt and Byword for my writing (LaTeX and Markdown respectively), so I didn’t think I could use his code.
Turns out I’m wrong.
The Script
Dr Drang seems to write most of his scripts in Python. My Python knowledge is sketchy at best - I could maybe write a Hello World script in Python, but that might be pushing it. In his post he states:
Although I use this in BBEdit, there’s no reason it couldn’t be adapted to other intelligent text editors. It uses standard input and standard output, so it ought to be easy to incorporate into a Sublime Text, TextMate, Vim, or Emacs workflow.
To me, that seemed like a challenge, as I wanted the benefits of the script, but I didn’t want to move to BBEdit - I had a quick look, but I think it wasn’t for me. For a start, it didn’t have the Cobalt theme (and a quick Google wouldn’t find one). However, the tempting nature of having this script run made me want to find a solution.
The key revolved around the fact it was a Python script. If I could pass the data to it, it would run and output as I would expect.
It took me an install of BBEdit to see how it worked in there before I could run it. Initially, I thought the script would run on a file after it was complete and make the change, but it relies on highlighting the text in BBEdit and then running the filter. Once I knew that, I realised I couldn’t run from the command line. Something niggled at the back of my mind though…
Automator + Services
I’d known about OS X services for a while - I know that Day One installs one, allowing you to create a note from any selected text, but I’d never really used them. However, they’re available in any app, so I thought this would hold the key. Turns out I wasn’t wrong.
I had to do a quick Google to find out how to create my own. It turns out services can be created using Automator.
Automator lets you link tasks together to perform them on occasions. These can be run on folders or items, or as it turns out, on selected text.
It’s a simple case of copying the code into a shell script task and selecting it to run in the Python shell. Select the option to receive input as text and to replace the selected text and it’s ready to run.
This then adds the option to run this within the Services menu. You can assign a keyboard shortcut to it using System Preferences, or in my case, I’ve done this using Keyboard Maestro as I only want it in some apps and I can change how it effects each one (and the keyboard shortcut if needed).
Overall, this seems to work well, though I realise it’s not as straight forward or as nice as Dr Drang’s BBEdit solution, but my workflow changes depending on where I am and what app I’m using, so this suits me better.