Any way to expose metadata in Markdown documents to DT?

This should work in a smart rule too

-- Use MultiMarkdown metadata for record properties

property theKeys : {"title", "tags"}

tell application id "DNtp"
	try
		set windowClass to class of window 1
		if {viewer window, search window} contains windowClass then
			set currentRecord_s to selection of window 1
		else if windowClass = document window then
			set currentRecord_s to content record of window 1 as list
		end if
		
		repeat with thisRecord in currentRecord_s
			set theText to plain text of thisRecord
			set theValues to {}
			
			repeat with thisKey in theKeys
				set lineStart to thisKey & ": " as string
				set foundValue to false
				repeat with thisLine in paragraphs of theText
					if thisLine starts with lineStart then
						set end of theValues to my replaceString(thisLine, lineStart, "")
						set foundValue to true
						exit repeat
					end if
				end repeat
				if foundValue = false then set end of theValues to ""
			end repeat
			
			set theName to item 1 of theValues
			if theName ≠ "" then set name of thisRecord to theName
			
			set theTags to item 2 of theValues
			if theTags ≠ "" then
				if theTags contains "," then
					set theTagList to my createList(theTags, ",")
				else if theTags contains ";" then
					set theTagList to my createList(theTags, ";")
				else
					set theTagList to {theTags}
				end if
				set tags of thisRecord to (tags of thisRecord) & theTagList
			end if
		end repeat
		
		
	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 replaceString(theText, oldString, newString)
	local ASTID, theText, oldString, newString, lst
	set ASTID to AppleScript's text item delimiters
	try
		considering case
			set AppleScript's text item delimiters to oldString
			set lst to every text item of theText
			set AppleScript's text item delimiters to newString
			set theText to lst as string
		end considering
		set AppleScript's text item delimiters to ASTID
		return theText
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't replaceString: " & eMsg number eNum
	end try
end replaceString

on createList(theText, theDelimiter)
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	set TextItems to text items of theText
	set AppleScript's text item delimiters to d
	return TextItems
end createList
9 Likes