Smart Rule to Search and Replace Markdown Contents

This should do it. Please try with duplicates.

-- Replace Obsidian image links with DEVONthink image links

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

tell application id "DNtp"
	try
		set theRecords to selection of think window 1
		if theRecords = {} then error "Nothing selected."
		
		repeat with thisRecord in theRecords
			set theType to (type of thisRecord) as string
			if theType = "markdown" or theType = "«constant Ctypmkdn»" then
				set theText to plain text of thisRecord as string
				if theText ≠ "" then
					set newText to my replaceImageLinks(theText)
					if newText ≠ theText and newText ≠ "" and newText ≠ "missing value" then set plain text of thisRecord to newText
				end if
			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
	end try
end tell

on replaceImageLinks(theText)
	try
		set ca to a reference to current application
		set theString to ca's NSString's stringWithString:theText
		set newString to theString's stringByReplacingOccurrencesOfString:("!\\[\\[(.*?)\\]\\]") withString:("![]($1)") options:(ca's NSRegularExpressionSearch) range:{location:0, |length|:length of theText}
		set newText to newString as string
	on error error_message number error_number
		error number -128
	end try
end replaceImageLinks
3 Likes