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.
Example
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
else
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>"})
else
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