Marked is a fantastic OS X application written by Brett Terpstra that will preview Markdown files. The primary use case is this: you have a Markdown file open in a text editor and you want to see the results of the HTML generated from the text. If you also have the Markdown file open in Marked then you see the HTML preview, nicely rendered. And every time the file gets saved, Marked will refresh the HTML preview.

I typically have BBEdit open on the left 60% of my screen and Marked open on the right 40% of my screen. One thing I quickly noticed was that it was a pain to switch files in BBEdit and then have to navigate to the same file in the open file dialog of Marked. So I wrote an AppleScript that will open the current file (if it is a Markdown or MultiMarkdown file) in Marked.

So, I created an AppleScript that would capture the path to the file currently open in BBEdit and open that file in Marked. The script is below. Using the AppleScript Editor, I saved it as ~/Library/Application Support/BBEdit/Scripts/Marked.scpt and it appeared in the scripts menu of BBEdit. But, before you do that yourself, know that a bit farther on I mention an alternative that might be more useful.

-- Open active file of BBEdit in Narked (if it is an .md or .mmd file(
--

set filePath to ""
tell application "BBEdit"
    set filePath to (file of front window) as text
end tell

set validMD to false
if filePath ends with ".md" then
    set validMD to true
else if filePath ends with ".mmd" then
    set validMD to true
end if

if not validMD then
    display dialog "Not a Markdown file: " & filePath buttons {"OK"}
    return
end if

tell application "Marked"
    open filePath
end tell

-- Change history
--
-- 2012-07-07 Created
--

That is a somewhat naïve script. It works for me – but after I wrote it I wondered if Brett Terpstra had anything like it on his site.

It turns out he has. Terpstra publishes many small utilities that focus on narrowly bounded creation and management tasks. Applicable to this case, there is a Marked Bonus Pack that contains both an AppleScript script and an Automator workflow for opening the current file in any application in Marked. Needless to say, it is a lot fancier and more robust than what I did; it has some special handling for certain applications, like the Finder and Emacs, that might have different semantics for “current file”.

Research topics

  1. Try Terpstra’s script and report back on how it worked for me.

  2. I’m also going to try the Markdown Service Tools which seem interesting. Especially the one called “Convert HTML to Markdown”.

  3. I’d like to find a way to have Marked open or activate whenever I switch to a new Markdown file in BBEdit. Or at least activate the already open Mark window, if one exists, whenever I switch to a Markdown file in BBEdit.