Incredibly useful feature of Marked that I just discovered!

I expect that many, if not all, MacPowerUsers are familiar with Brett Terpstra and his excellent Marked app. I have Marked running pretty much all the time so I can preview whatever markdown document I am writing or referencing. I bought a license before discovering that it was also a part of SetApp, but no regrets - it is useful enough to have paid for twice!

While I generally use Marked in its most obvious mode, where it monitors a file and detects whenever the file is changed and updates its display, I just discovered that Marked can also monitor a special system clipboard named “mkStreamingPreview.” You open a special document window that displays the contents of this clipboard via the Preview > Streaming Preview menu command.

The idea is that apps which are aware of this feature can write markdown formatted output to this clipboard resulting in a live-updating preview of the markdown content. This is a very cool feature, but I found a use for it that solves a specific problem for me in a very cool way.

I am working on a data analysis task, for which I have imported the relevant data into an sqlite database from which I can run various queries on demand. What I wanted to be able to do is to hook up my laptop to a TV in a conference room, run the queries, and have the output displayed in a readable format on the TV.

Sqlite can be set to output in html or markdown format, but having queries quickly display on the TV in a non-obfuscated manner (eg not showing the terminal window in which I am working on the queries, for instance) and in a reasonably formatted display provided to be difficult.

I thought I could just output html output and pipe it onto Safari, eg:

sqlite3 databasefile SQL_query | open -a Safari -f

but this does not work. It turns out that open saves the input from standard input to a temporary file with a .txt extension and sends that to Safari, which displays html in a .txt file as the actual html text, rather than interpreting the html for proper (formatted tables) display.

After discovering this Marked feature, it made sense to output the results of the sqlite query in markdown format and pipe it into this clipboard. That way, each time I run a query, the Marked display window (on the TV) updates accordingly.

I thought the pbcopy command would do the trick with the -pboard option, but it turns out that option only appears to allow you to copy to a set of standard system clipboards (general, ruler, find, font) but not any arbitrary clipboard, so that did not work.

Fortunately, Brett kindly provided in the documentation the code for putting text on the proper clipboard, which I modified to put text read from standard input (and also translated to Swift; his code is in Objective-C but I have pretty much moved this kind of coding to Swift). Using this simple app called markedstream (since it updates the Marked streaming clipboard), I can use:

sqlite3 -markdown databasefile SQL_query | markedstream

to accomplish what I need to do.

This is admittedly very simple, but this incredibly versatile app (Marked) gave me a very useful solution for this task and I expect to find more uses for this feature.

For anyone who needs it, the very simple Swift code for markedstream is:

import AppKit

if let data = try? FileHandle.standardInput.readToEnd() as NSData? {
    if let dataString = String(data: data as Data, encoding: .utf8) {
        let pb = NSPasteboard(name: NSPasteboard.Name("mkStreamingPreview"))
        pb.clearContents()
        pb.setString(dataString, forType: .string)
    }
}
9 Likes

What a great write up of a great idea! Please also post it on Brett’s Discourse forum. I’m sure he’d love to see what you built.

You just gave me an idea for two of my own apps just now……

Thanks!

3 Likes

This is great! I’m working on a CLI to include with Marked 3 that has similar functionality. BTW, if you’re interested in the beta, the TestFlight is public, just visit https://markedapp.com/join-the-beta!

5 Likes

And if you do want to share it with my readers and other Marked users, the forum is at https://forum.brettterpstra.com. Lisa has already started a discussion here.

Big fan of Marked and its streaming preview, which I’ve used in Curio for a long time.

Brett – you’re a solid internet citizen in many ways!

Katie

See related post on @ttscoff’s forum, https://forum.brettterpstra.com/t/streaming-preview-and-pasteboards/4434