Scripting Question - Creating a new Document with bold, italics, maybe colors

I am writing a script to summarize information in DT3.

I have most of the logic working but am trying to determine the best way to programmatically create the output documents. I am using JXA but I think the question I am asking here is likely equally applicable to JXA or Applescript.

If I use createRecordWith it gives me options including rich text - but there is no option to create an HTML or Markdown document. Why is that?

Assuming I create a Rich Text document, how do I specify use of headings, bold/italics, links, and perhaps color in the code that creates the document? From the documentation it seems as if I may need to create Create text objects but I am not sure what the syntax would be there.

Let’s suppose I create to do the equivalent of this Markdown text:

This is a test

##This is a heading

This is **bold**

This is *italics*

<span style="color:blue">some *blue* text</span>

[This is a link to CNN](

Which should ultimately render like this in a Markdown editor:

How would I create this programmatically in JXA?

My experience is with Applescript and format: formatted note (html)
html code is well documented, and easily assembled in a script

I can also create a note using the Devonthink editor,
and then view the underlying code
using an external text editor

OK so I set the record.type to “Formatted Text” or “HTML” or “Markdown” I presume?

Then where does the HTML or Markup text go?

Perhaps in record.richText? record.plainText?

Here’s a sample script

[details=“Click to see script”]

set theNoteName to "0000-00-00 ProjectToC [" & projectName & "]" 
set theSource to "<html>
	<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>
	<meta name=\"DT:isEditableNote\" content=\"Yes\"/>
	<body style=\"background-color:ivory; margin:1.5em; font-family:'Times-Roman','Times'; font-size:14px\">
		<table style=\"border: 1px solid black;\">"
set theSource to theSource & "<h3>" & theHeading & "</h3>"
set theSource to theSource & "<a href=" & (item 2 of theNote) & ">" & (item 1 of theNote) & "</a><br>"
set theSource to theSource & "</tr></table></body></html>"

tell application id "DNtp"			
    set theFilingGroup to get record with uuid "AA28F696-D9F9-4FC3-9089-B1EDBC96011E" in database "FilingCabinet" ------ Create Note
    set newRecord to create record with {name:theNoteName, type:formatted note} in theFilingGroup
    set source of newRecord to theSource
    set tags of newRecord to {"cType-ProjectToC", theProjectTag}
end tell


OK that is very helpful

That tells me that if I want to create an HTML document then I need to set record.source to the raw HTML.

What field in the record do I use for content if I want to create a Markdown document or a Rich Text document instead?

You had that covered with "record.type to “Formatted Text” or “HTML” or “Markdown”

That just designates the type of record it is.

Does the actual content for Rich Text, Formatted Text, or Markdown go in record.Source or in some other metadata field?

Confirmed; You should only need type and source

The AppleScript/Devonthink Data Dictionary provides more information on record fields

That seems to be the same as the scripting dictionary in Script Editor with a slightly different presentation of the same information.

The question becomes - when do we place new text into record.source as opposed to record.richText or record.plainText?

If I wish to append to the end of an existing document, can I do so by concatenating the current contents of record.source with the new content?

For markdown, I’m always using plainText. Maybe source is also possible.

To append to the existing document- you just concatenate with what’s already there?

record.plainText = record.plainText() + …

1 Like