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
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.
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…