Script: universal formatting shortcuts for markdown and rtf

Disclaimer first:
(1) I have never written a single line of html in my life and have only heard of markdown until in the last few months. To me, md specific syntax and html tag are just elements of a portable file format. It is the universal css of md that attracts me to consider switching my notes taking to markdown. The technique this script uses is a “Frankenstein” of markdown and html syntax. So please don’t judge me - because I am not suggesting this script is a “better” or “faster” way to do things AT ALL. In fact, it may “contaminate” the institutionalized way of using markdown.

(2) However, this script is fitting my purpose perfectly. I have yet to be convinced to use markdown for short-note taking (I use DT for knowledge retrieval and notes taking, and only use Scrivener for long writing. I don’t need to integrate all sort of info on my computer and web at all.). So I need to find a way to use rtf and markdown consistently, both in terms of (1) ways to format and (2) the consistency of notes’ format. With (1) and (2), I can always use one method for notes taking and retain the same format in notes if I change my mind on rtf or markdown later.

(3) Some may think it’s a waste of time to script, particularly for my type of script. It is not, my first two real projects “Stack” and “Tagger” are long and complicated, but these two projects and my other scripts give me a large number of handlers and std codes that can snap and fit in a short time. For example, I use less than 90 mins to put this script together. I guess that’s one of the main reasons for having OOP?

(4) As pointed out by many users, there are many excellent apps that can do way better jobs than my scripts (whatever better means is different for different users) - and I agree. The purpose of my scripting is a journey to learn about Applescript (and the fun of programming) and to understand the mezzanine layer of DT’s internals. Besides, I feel some of the handlers, the way I manipulate records and arrays, and the concept of flexible configurations may be of help to those who are starting/wanting to know more - just like myself a year ago.

What the script does:
It’s a “seed” script, I make multiple copies of it and choose an option in one property. And I get up to seven formatting shortcuts that can be applied to both rtf and md files.

T-Normal text (or restore to normal text), U-Underlying, B-Bold, S-Section heading, C-Chapter heading, R-Color text, H-Highlight text.

These are the original text in rtf and md file:

And I use exactly the same keystrokes to format the two files within DT.
Noted that the blue-color texts are not links, they are colored text by the script.

Noted that:
(1) I use a slightly modified css “Modest” from Typora - recommended by a very friendly forum member. Therefore, the font-face, ratio of font-size for rtf formatting mimic the css in the script.
(2) According to the valuable advice of many experienced md users, tt’s better to use direct syntax entry or external (WYSIWYG or not) editor if markdown is the committed format. I am still flipping between rtf and markdown, and I frequently need to format those cited texts in the notes
(3) I enjoy walking around, and DT is my only major interface of info. So, please bear in mind that I am not aiming for efficiency in development time, the correctness of using a tool, or picking the best tools for the job. I am merely enjoying my time and hope some of my codings can help others as the others have once helped me.

Cheers (time to get back to the real work…)

The only option that needs to be flipped is “property setStyle:” for each copy of the script. Users will need to change the font face and font size for their preference.

property setStyle : "T" -- "T","U","B","C","S","H","R"

The seed script:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--By ngan 2020.03.14 

property setStyle : "U"

-- {font face, font size, text color, highlight color, underlined, beginning and ending tag for markdown}, for attributes that don't need to change, leave them as empty ""
property styleT : {"SFProText-Light", 12.0, {13448, 13448, 13448}, {65535, 65535, 65535}, false, {"", ""}}
property styleB : {"SFProText-Semibold", 12.0, {13448, 13448, 13448}, "", false, {"**", "**"}}
property styleS : {"SFProText-Light", 18.0, {13448, 13448, 13448}, "", false, {"#### ", ""}}
property styleC : {"SFProText-Light", 24.0, {13448, 13448, 13448}, "", false, {"### ", ""}}
property styleR : {"SFProText-Light", 12.0, {0, 0, 65535}, "", false, {"< font color=\"Blue\">", "</font>"}}
property styleU : {"SFProText-Light", 12.0, {13448, 13448, 13448}, "", true, {" <u>", "</u>"}}
property styleH : {"", "", "", "yellow", false, {" <mark>", "</mark>"}}

property RTFStyleList : {{"T", styleT}, {"B", styleB}, {"S", styleS}, {"C", styleC}, {"R", styleR}, {"U", styleU}, {"H", styleH}}

global theEraser

tell application id "DNtp"
	set theWin to think window 1
	set theDoc to content record of think window 1
	--Repeat with each in 
	if kind of theDoc is "Rich Text Document" then
		if setStyle is in {"T", "B", "S", "C", "R", "U", "H"} then
			set theStyle to my lolLookup(setStyle, 1, 2, RTFStyleList)
			if theStyle's item 1 is not "" then set font of (selected text of think window 1) to theStyle's item 1
			if theStyle's item 2 is not "" then set size of (selected text of think window 1) to theStyle's item 2
			if theStyle's item 3 is not "" then set color of (selected text of think window 1) to theStyle's item 3
			if theStyle's item 4 is not "" then set background of (selected text of think window 1) to theStyle's item 4
			-- very strange, if I use {underlined of (selected text of think window 1), theStyle's item 4} the comparison won't work!
			set {a, b} to {underlined of (selected text of think window 1), theStyle's item 5}
			if {a, b} = {true, false} then
				set underlined of (selected text of think window 1) to true
				set underlined of (selected text of think window 1) to theStyle's item 5
			end if
		end if
	else if kind of theDoc is "Markdown" then
		set theStyle to my lolLookup(setStyle, 1, 2, RTFStyleList)
		set bTag to item 1 of (theStyle's item 6)
		set eTag to item 2 of (theStyle's item 6)
		if setStyle is "T" then
			set (selected text of think window 1) to my TrimText((selected text of think window 1), {"*", "#", "==", "<u>", "</u>", "<font color=\"Blue\">", "</font>", "<mark>", "</mark>"})
			set (selected text of think window 1) to bTag & (selected text of think window 1) & eTag
		end if
	end if
end tell

on lolLookup(lookupVal, lookUpPos, getValPos, theList)
	--only for list of list with more than 1 items
	local i, j, k
	set j to lookUpPos
	set k to getValPos
	repeat with i from 1 to length of theList
		if (item j of item i of theList) is equal to lookupVal then return item k of item i of theList
	end repeat
	return {0, {}, {}}
end lolLookup

on TrimText(theText, theTrim)
	set AppleScript's text item delimiters to the theTrim
	set the item_list to every text item of theText
	set AppleScript's text item delimiters to the ""
	set theText to the item_list as string
	set AppleScript's text item delimiters to ""
	return theText
end TrimText

1 Like

I really don’t think you need to apologise for spending your time creating and sharing your scripts! People can use them or not as they see fit, but even so, the way you go about solving the problems is helpful as a resource for others to have.

Thank you.


:grinning: :sweat_smile: :pray:

1 Like

Out of all the scripts you people have seen from me, there are very many you haven’t. Some are purely academic; some are just like this one - it scratched an itch of mine. There’s nothing wrong with stretching your legs with some scripting. And if someone can use it or glean something from it, that’s icing on the cake.


1 Like

It may sound strange. When I first picking up Applescript after not doing any VBA programming for 20 years, every command and syntax looked alien to me. It took me three days to know that I need to add “my” before a handler to make it work. Now, it’s like playing with a puppy - for DT related scripting .

It’s simple (and powerful) once you get to know it.




A follow up review of using markdown.

Regarding the universal format. I have been using markdown(only) for notes taking for the last few weeks and I think I will stick with this format. Initially, I feel that all the markings (*,#,,links in []()) are disruptive to my reading after I format the notes. But my cognition filter began to kick in after a few weeks and those markings have gradually dissolved into background. Knowing that all my notes will now have the duality of portability and the flexibility to change the format of all notes at once really make me a happy man.

Regarding the formatting. The more I use it, I more I think external WYSIWYG markdown editor is becoming(perhaps) unnecessary BUT ONLY when and if I am using my scripts for the formatting. I think the workflow of typing while formatting is perhaps suitable for straight writing, but I feel that formatting functions really make the workflow of simultaneous citing text and notes taking much easier.

I guess what I am trying to say is that markdown is a future-proofed format (IMHO), but some handy formatting functions (through a WYSIWYG editor or scripts or KM shortcuts or DT’s future built-in format bar for markdown) are still necessary for some type of users who prefer to perform formatting after writing/reviewing.

The final experiment I am still working on is to find out which image link method is most suitable in DT environment. Among using DT-link, relative link, and absolute-path link methods, and assuming that DT is being used as the main container of all info, I incline to say that DT-Link is the most appropriate method due to the compatibility of sharing the image in both DT3 and DTTG. While some external apps (I only test Mac OS apps) may not be able to display DT-Linked image, I can still activate the link to open the image in DT.

Just my 5 cents.