Script to replace text in given selection of records

I’m looking for a script to replace some text with some other text in the contents of all records of the current selection (e.g. a Smart group with all Markdown documents).

Is there anything ready-made for this? I looked at the Rename script, but this only changes at the names of records not the content

Another thing I found in forum is that I can modify the files in the database bundle with an external tool/editor, but this was a post from 2009 - is this still valid?

I guess a DT-builtin Applescript is the safer alternative…

Here’s a script based on the “Replace text in names” script. Warning: This can’t be undone.


-- Replace Text in Plain Text & Markdown documents
-- Created by Christian Grunenberg Tue May 16 2017.
-- Copyright (c) 2017. All rights reserved.

tell application id "DNtp"
	try
		set this_selection to the selection
		if this_selection is {} then error "Please select some contents."
		
		repeat
			set search_string to display name editor "Replace Text" info "Enter text to find:"
			if search_string is not "" then exit repeat
		end repeat
		
		set replacement_string to display name editor "Replace Text" info "Enter replacement text:"
		
		set od to AppleScript's text item delimiters
		repeat with this_item in this_selection
			set this_type to ((type of this_item) as string)
			if this_type is "markdown" or this_type is "text" then
				set current_text to plain text of this_item
				if current_text contains search_string then
					set AppleScript's text item delimiters to search_string
					set text_item_list to every text item of current_text
					set AppleScript's text item delimiters to replacement_string
					set new_item_text to text_item_list as string
					set plain text of this_item to new_item_text
				end if
			end if
		end repeat
		set AppleScript's text item delimiters to od
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

1 Like

I’ll check it out. Thanks for this.

I’m interested in such script as well for potential future use, please let the forum know whether it works for you!

Do I understand correctly that the purpose of this script is to alter text in more than one document within a Group? This is something I would have occassional use for and I hve tried it but nothing appeared to have happened. I may, of course, have misinterpreted the purpose or executed it incorrectly. To run the script I selected all the items, (all .rtf’s) and then ran the script.

The script supports only plain text and Markdown.

That would explain it then! Thanks for your quick response. :slight_smile:

Finally I got around to the Replace script - and it doesn’t work for me.

Here my steps what have I done to use it:

  • Saved the script in the script editor as “Replace in Text Records.scpt” in the DEVONthink script folder
  • Selected in DEVONthink two Markdown documents - one containing the word “modular”, one without this word.
  • Selected the script in DEVONthink’s script menu.
  • Waited…
  • Cleared selection and selected the document which had contained “modular” and put the document in edit mode, but it still contains the word “modular”.

Any idea what’s going wrong?

What did you enter in the dialogs??

Ups, missed that step:

In “Enter text to find” I type “modular”,
in “Enter replacement text” I type “compartmentalised”.

Relaunch DEVONthink and run it again.

Restarted DTPro - but does not better it.

I just saw in the script that the type strings “markdown” and “text” are lower case, but in DT the Kind is capitalised - does this make a difference?

Kind and Type are not the same thing.

I am running Criss’ script as is on a Markdown document and it’s changing the word as expected. Note the string to match is case-sensitive.

Just send you via email a screencast of what I’m doing - maybe this sheds light on the issue.

Well, I have restarted my machine, restarted DTPro, but still the script doesn’t do any text change for me (still on 2.9.11). I hope other people have more luck.

For my bulk editing needs I’m now rather thinking of changing the underlying files in the file system with a bulk editor…

Why? You should update to 2.9.12.

Because of this:

Just for the record:

The script does not work for me even with 2.9.12. Something must go wrong in my environment.

Does something like this exist to do a “bulk edit” or bulk replace of metadata in a selection of multiple documents of any type?

What might be very nice is an option for “Paste from Clipboard” as a placeholder in a Smart rule - but I do not think this is available currently.