DEVONthink 3.8 - Added script Scripts > Edit > Replace Text in Documents

Do you have an effective way to use an external editor when doing batch changes over results from a search in DEVONthink?

I normally wouldn’t resort to a script like this in the first place, but I had a situation where I needed to replace something in a hundreds of markdown files spread all over a database. I did it by (1) running a search in DEVONthink for the text to be replaced, then (2) selecting all the search results in the DEVONthink window, and (3) applying a modified version of the AppleScript script to the results.

In a situation like that, have you found an effective way to both invoke an external program (say, VS Code) and programmatically instruct it to perform a batch find/replace operation on hundreds of files? I would be interested in learning your approach for that.

1 Like

This script opens the selected records in BBEdit.

You can then use BBEdit menu Search > Multi-File Search.


Note: The script needs BBEdit Command Line Tools, see BBEdit menu BBEdit > Install Command Line Tools...


-- Open selected records in BBEdit for Multi-File Search
-- Note:	You need to install BBEdit Command Line Tools, see BBEdit menu "BBEdit > Install Command Line Tools..."
-- Usage:	Select records in DEVONthink. Run script. Use BBEdit menu "Search > Multi-File Search". Select the desired windows under "Search in:" > "Open Editing Windows"

tell application id "DNtp"
	try
		set theRecords to selected records whose path ≠ ""
		if theRecords = {} then error "Please select some records"
		
		set thePaths to {}
		
		repeat with thisRecord in theRecords
			set end of thePaths to path of thisRecord
		end repeat
		
		set thePaths_string to "'" & my tid(thePaths, "'" & space & "'") & "'"
		
		do shell script "echo \"\"  | /usr/local/bin/bbedit --clean && /usr/local/bin/bbedit --front-window " & thePaths_string
		
	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 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
2 Likes

I don’t, but apparently @pete31 had. And I suppose something similar could be concocted for other editors.