Convert hashtags found in text to tags

It’s only possible to use a modified version of the script if you create a script library as it’s not possible to use the previous script from within a Smart Rule.

  1. Save this script to /Users/USERNAME/Library/Script Libraries/ (might be necessary to create this folder) and name it exactly Script Library - regexFind.scpt
-- Script Library - regexFind

use AppleScript version "2.7"
use framework "Foundation"
use scripting additions

on regexFind(theText, thePattern)
	try
		set aString to current application's NSString's stringWithString:theText
		set {theExpr, theError} to current application's NSRegularExpression's regularExpressionWithPattern:(thePattern) options:0 |error|:(reference)
		set theMatches to theExpr's matchesInString:aString options:0 range:{0, aString's |length|()}
		set theResults to {}
		repeat with aMatch in theMatches
			set theRange to (aMatch's rangeAtIndex:0)
			set theString to (aString's substringWithRange:theRange) as text
			set end of theResults to theString
		end repeat
		return theResults
	on error error_message number error_number
		activate
		display alert "Error: Handler \"regexFind\"" message error_message as warning
		error number -128
	end try
end regexFind

  1. Create a Smart Rule that searches Markdown records, this new script doesn’t check for the record type as it’s unnecessary in a Smart Rule.
-- Smart Rule script - Convert hashtags found in a record's text to tags (and create nested tags)

on performSmartRule(theRecords)
	tell application id "DNtp"
		try
			repeat with theRecord in theRecords
				set theText to plain text of theRecord
				
				-- I didn't use a lookbehind assertation as it's probably better to remove the # afterwards in AppleScript
				set theHashTags to script "Script Library - regexFind"'s regexFind(theText, "\\#(.*?)(?=\\s|$)")
				
				set theHashTags_clean to {}
				repeat with thisHashTag in theHashTags
					set thisHashTag to thisHashTag as string
					try
						set thisHashTag_clean to characters 2 thru -1 in thisHashTag as string
						if thisHashTag_clean does not contain "/" then
							set end of theHashTags_clean to thisHashTag_clean
						else
							create location (("/Tags/" & thisHashTag_clean) as string) in (database of theRecord)
							set end of theHashTags_clean to (item -1 of my tid(thisHashTag_clean, "/"))
						end if
					end try
				end repeat
				
				set tags of theRecord to (tags of theRecord) & theHashTags_clean
			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
end performSmartRule

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