Open a Markdown file in Marked for previewing

This script will open a plain text file for previewing the rendered text in Brett Terpstra’s Marked app (esp. Markdown, MultiMarkdown, Textile, or even custom tag processor). Marked offers a range of features, including sending rendered text to clipboard, PDF or printer in various formats. You may apply custom CSS to the preview if desired.

When the file is open in Marked you may continue editing in DEVONthink. When changes are saved, the Marked preview updates. Putting an editor window and a Marked window side by side is the common use case.

I have the script installed in a Keyboard Maestro palette. You could also give it a keystroke shortcut, or install it in the DEVONthink menubar.

This script has limited error checking. Use at your own risk.

(*
	Script that will open a plain text file in Brett Terpstra's Marked
	application for viewing.  See http://markedapp.com for details.
	
	20130629 adjusted to account for Markdown handling in 2.6 build 2
*)

tell application id "DNtp"
	try
		if selection is {} then error
		set theItem to (the first item of (the selection as list))
		set theKind to kind of theItem
		if the theKind is not in {"mkdown", "markdown", "md", "mmd", "multimarkdown"} then error
		set thePath to the path of the theItem
		tell application "Marked"
			activate
			open thePath
		end tell
	on error
		display dialog "Please select a text file" & return & "Marked works only with plain text"
	end try
end tell

Thanks for sharing this, Korm.

Tangentially, I do something similar, combining it with checking that the text file is also open in the relevant editor, as well as in the Marked previewer.

I find that Marked, presumably because it asks for read-only access, works equally well with such scripts in open and locked databases, but I have had intermittent issues with script-triggered opening of text files in databases which have a username and password set, even though I have entered a password to open the database.

Console errors refer to sandboxd …

Not sure if others have seen this.

(My code, FWIW, is below - the extra wrapping around the opening by the text editor is just a part of trying to debug what is going on …)

property pTitle : "Open in text editor and Marked"
property pVer : "0.01"

tell application id "DNtp"
	set {lstSeln, strPath, strType} to {selection, "", ""}
	if lstSeln ≠ {} then tell item 1 of lstSeln to set {strPath, strType} to {path, kind}
	if strType ≠ "Text" then return
end tell

if strPath ≠ "" then
	-- OPEN MARKED
	tell application id "com.brettterpstra.marky"
		open strPath
		activate
	end tell
	
	-- AND THE TEXT EDITOR
	if FileExists(strPath) then
		set oFile to POSIX file strPath
		tell application id "com.foldingtext.FoldingText"
			open oFile
			activate
		end tell
	end if
end if

on FileExists(strPath)
	(do shell script ("test -e " & quoted form of strPath & "; echo $?")) = "0"
end FileExists

Nice addition, Rob.

Over here, I would change the editor to “com.multimarkdown.composer” (my personal preference for MMd editing), or add a dialog to choose the editor – rather than hardcoding FT.

Absolutely - I did experiment with setting the name of an app in a property and then using an eval (a run script statement on a string assembled-script) but it slows things down, and run script is probably not a good thing to over use …