AppleScript: create new markdown record with selected records as transclusions

With a big debt of gratitude owed to my teacher and mentor, @Bluefrog :pray:, here is a refined version 2.0 of the script. It now uses set recURL to reference URL of theRecord to obtain the DT item link directly (and hence dispense with construction of the link, as in version 1.0). In particular (with a noticeable increase in speed when many records are selected for transclusion) it incorporates an empty list for the item links (set textUpdates to {} and set plain text of theNewRecord to (textUpdates as string)) so enabling a single update of the new record when all item links have been gathered.

(* This script creates a new markdown record in the chosen database
and group and then adds to that record transclusion item links for plain text,
markdown or HTML records you have selected in the database with a line drawn
between each item. It will prompt you for the name of the new record or
you can choose to use the default name. *)

-- the database to open
property pDatabase : "/Users/[path to database you wish to use]/*.dtBase2"
-- the group to use for tbe markdown file of transclusions
property pGroup : “[UUID of relevant group]”
property validTypes : {"markdown", "txt", "html"}

tell application id "DNtp"
	try
		activate
		-- open the database
		set theDatabase to open database pDatabase
		set myGroup to get record with uuid pGroup
		
		--check that we have some records selected for transclusion
		set theSelection to the selection
		if (count of theSelection) is 0 then
			error "Please select some markdown records for transclusion"
		end if
		
		--set up the new record for the transclusions
		set dAnswer to "New document"
		set userEntry to display dialog ¬
			"Enter the name of the new document (without the extension) or accept the default: " default answer dAnswer with title "New document for the transclusions"
		set dAnswer to text returned of userEntry
		set theNewRecord to create record with {name:dAnswer, type:markdown} in myGroup
		
		set textUpdates to {} -- Set up an empty list to populate with new strings, etc.
		repeat with theRecord in (selection as list)
			if (type of theRecord as string) is in validTypes then -- Check the document type
				--Get the reference URLs of the selected records, convert them to item links
				--and format the transclusions
				set recURL to reference URL of theRecord
				set transcludedItem to my createTransclusion(recURL)
				copy (transcludedItem & linefeed) to end of textUpdates -- Copy the string to the list, noting a linefeed is added after each line
			else
				error "Check selected records: only markdown, plain text and HTML records are suitable for transclusion"
			end if
		end repeat -- Keep adding to the list until the loop expires
		
		set plain text of theNewRecord to (textUpdates as string) -- Update the text of the file in one move
		
		set root of viewer window 1 to myGroup
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

--The handler to format the transclusion links is separate simply to make it easy
--to change the formatting between items if you wish.
on createTransclusion(theURL)
	set theLink to "{{" & theURL & "}}" & linefeed & linefeed & "---" & linefeed
	return theLink
end createTransclusion

Stephen

1 Like