Hacking around with Marked’s Streaming Preview
In the last Marked newsletter I wrote a bit about Marked’s Streaming Preview, a feature of Marked that flew under the radar for a few years before starting to gain some traction.
It’s a special window (Preview->Streaming Preview) that watches a private clipboard for updates, updating the preview every time the clipboard contents change. All an app needs to do to interface with it is write Markdown content to the named clipboard, which is easily done from any Swift/Objective-C instance. Normally Marked watches files on the disk for changes, but this special window allows apps to update the preview in Marked without ever writing to disk, and with closer-to-live update speeds. Apps like Drafts, Bear, and The Archive have all implemented it with great results. (I even added some special processing for Bear users, so a majority of the Polar syntax translates to your live Marked 2 preview.)
Because the only trick to using the Streaming Preview is to write to a clipboard, it’s possible to do it in your own scripts as well. You just need to write to a pasteboard named “mkStreamingPreview” and Marked will do the rest.
As an example, Bruno Conte created a Keyboard Maestro macro that will take any currently-selected text and update the Marked preview. Marked has a “Preview Clipboard” command, of course, but that requires copying the text, switching to Marked, and hitting ⇧⌘V — and it opens a new window every time. This macro means you can just select text and hit a keyboard shortcut to live-update the preview window from any application.
Here’s the Keyboard Maestro macro: Copy and Stream to Marked. In order to use it, you’ll need Python’s PyObjC
module installed, so if needed, open Terminal and run: pip install PyObjC
.
The crux of the macro is in the Python script:
def stream_to_marked(data):
from AppKit import NSPasteboard
pb = NSPasteboard.pasteboardWithName_("mkStreamingPreview")
pb.clearContents()
pb.setString_forType_(data.decode('utf-8'), 'public.utf8-plain-text')
You can incorporate this kind of script in whatever tools you like. The macro also makes use of Marked 2’s url handler for opening the Streaming Preview externally: just call open x-marked://stream/
.
See the docs for more implementation details (including settings a base URL for relative paths). If you come up with cool new ways to use this, I’d love to hear about it. And if you’d like to see Marked’s Streaming Preview integrated into your favorite app, just put the developers in touch with me!
Want to get Marked tips (and special discounts) in your inbox? Sign up for the mailing list!