Script: Copy for Markdown transclusion

This script copies selected records for Markdown transclusion.

Setup

Set property theLinkType to your preferred type:

  • 1 = DEVONthink reference URL
    {{x-devonthink-item://A21FAE04-A818-42F8-B0CC-2B137F11AB2E}}

  • 2 = DEVONthink location
    {{/Test/Hello World.md}}

  • 3 = File system path (if indexed)
    {{/Users/User/Desktop/Indexed folder/Hello World.md}}

Supported file types

  • Markdown

  • Text

  • HTML

  • RTF

  • RTFD

  • Formatted Note

  • Webarchive

  • Sheet

-- Copy for Markdown transclusion

property theLinkType : 1 -- Set link type. 1 = Reference URL, 2 = Location, 3 = Path (if indexed)

tell application id "DNtp"
	try
		set theRecords to selected records
		if theRecords = {} then error "Please select a record"
		
		set theTransclusionList to {}
		
		repeat with thisRecord in theRecords
			set thisType to (type of thisRecord) as string
			if thisType is not in {"markdown", "«constant ****mkdn»", "txt", "«constant ****txt »", "html", "«constant ****html»", "rtf", "«constant ****rtf »", "rtfd", "«constant ****rtfd»", "formatted note", "«constant ****DTft»", "webarchive", "«constant ****wbar»", "sheet", "«constant ****tabl»"} then error "Unsupported: Record type not Markdown/Text/HTML/RTF/RTFD/Formatted Note/Webarchive/Sheet"
			
			if theLinkType = 1 then
				set end of theTransclusionList to reference URL of thisRecord
				
			else if theLinkType = 2 then
				set thisName to name of thisRecord
				if thisName contains "/" then set thisName to my escapeSlash(thisName)
				set end of theTransclusionList to ((location of thisRecord) & thisName) as string
				
			else if theLinkType = 3 then
				if not indexed of thisRecord then error "Unsupported: Record not indexed"
				set end of theTransclusionList to path of thisRecord
			end if
		end repeat
		
		set the clipboard to ("{{" & my tid(theTransclusionList, "}}" & linefeed & "{{") & "}}")
		display notification "Copied"
		
	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

on escapeSlash(theText)
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "/"
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to "\\/"
	set theText_escaped to theTextItems as string
	set AppleScript's text item delimiters to d
	return theText_escaped
end escapeSlash

on tid(theInput, theDelimiter)
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	if class of theInput = text then
		set theOutput to text items of theInput
	else if class of theInput = list then
		set theOutput to theInput as text
	end if
	set AppleScript's text item delimiters to d
	return theOutput
end tid

3 Likes

Thanks for sharing. Very useful.

1 Like

Hi Pete,
Thank you for sharing.
What is the purpose of this script?
How can we implement it in DT?
Sorry for my noob questions :pleading_face:

For one (or more) selected records it sets the clipboard to the text that’s necessary to add a transclusion in a Markdown record. In your thread you wrote

You have to somehow build the string:

{{ + location + name + }}

If you set property theLinkType to 2 this script copies this kind of transclusion link.

  • Paste in Script Editor.app
  • Save to DEVONthink’s script folder
    ~/Library/Application Scripts/com.devon-technologies.think3/Menu

You can add a shortcut to the script’s filename, e.g. MyScript___CTRL + C.scpt. That’s three underscores, then the shortcut, then the extension. It’s simpler to use CustomShortcuts.app. If you use CustomShortcuts just start typing the name of the script.

1 Like

Perfect :ok_hand:
I will definitely use it.
Thank you, Pete, for your help

DEVONthink 3.8.1 added support for several file types:

Markdown transclusion supports now also RTF/RTFD documents, formatted notes, web archives, and sheets. These documents are automatically converted to Markdown.

Thanks a lot!

Updated the script