MarkdownLib is a small AppleScript library for converting bodies of styled text into Markdown and is part of my AppleScript Libraries repository on GitHub.
The library can be used with any application that supports the Text Suite (the Text Suite’s attribute run
object in particular). This includes TextEdit, all of the OmniGroup’s applications, and many others. I’m a heavy user of OmniOutliner and MarkdownLib can be used to covert an outline and its notes into Markdown. Sadly, Apple’s Pages does not provide an attribute run
object.
Installation
Download and run this script application:
Install AppleScriptLibraries.zip (71.1 KB)
NOTE: MarksLib is required for the example scripts that follow. MarkdownLib can be used without MarksLib.
Installation (manually)
Enter the following commands in the Terminal application to install the latest version of MarkdownLib and MarksLib on your machine:
curl https://raw.githubusercontent.com/alldritt/AppleScriptLibraries/master/MarkdownLibLib.applescript | osacompile -o ~/Library/Script\ Libraries/MarkdownLib.scpt
curl https://raw.githubusercontent.com/alldritt/AppleScriptLibraries/master/MarksLib.applescript | osacompile -o ~/Library/Script\ Libraries/MarksLib.scpt
Usage
MarkdownLib provides the following handlers:
richTextToMarkdown
The richTextToMarkdown(richTextReference)
handler converts the referenced rich text object into a Markdown string and returns it as the handler’s result.
Example
This example script converts the contents of a TextEdit document to Markdown.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use MarksLib : script "MarksLib" version "1.0"
use MarkdownLib : script "MarkdownLib" version "1.0"
use scripting additions
tell application "TextEdit"
set theName to name of document 1
set theMarkdown to MarkdownLib's richTextToMarkdown(a reference to document 1)
end tell
MarksLib's writeToFile("~/Desktop/" & theName & ".md", theMarkdown)
TextEditMarkdownDemo.rtf.zip (1.4 KB)
Open this sample document in TextEdit.
Running the AppleScript code shown above produces the following output (viewed in MacDown):