Today, I imported about 25 feed entries prefixed with the name of the feed (‘Long Feed Name: entry 1’, ‘Long Feed Name: entry 2’, etc.). I didn’t like that prefix and wanted to remove it. Unfortunately, the ‘Replace Text in Names’ script that executers the ‘Replace Text…’ function in DEVONThink’s Scripts Menu (under Rename) doesn’t allow for replacement by an empty string, so it can’t be used for this purpose. It was not the first time that I encountered this problem, so I wrote my own script to solve this problem.
-- Rename selected items
-- Replaces a string in the names of the selected items by a replacement string (case sensitive)
-- Asks for both the string to replace and the replacement string
-- The replacement string can be empty
-- Arno Wouters, 8 July 2013
tell application id "com.devon-technologies.thinkpro2"
try
set theSelection to the selection
on error errMsg number errnum
if errnum = -1700 then
error "Please select one or more documents before running this script."
else
error "Error " & errnum & ": " & errMsg
end if
end try
set toReplace to ""
repeat until toReplace is not "" (* don't try to replace an empty string!*)
display dialog "Enter text to replace: " default answer ""
set toReplace to the text returned of the result
end repeat
display dialog "Enter replacement: " default answer ""
set Replacement to the text returned of the result
repeat with thisEntry in theSelection
set thisName to the name of thisEntry
try
set the name of thisEntry to my findReplace(thisName, toReplace, Replacement)
on error errMsg number errnum (* keep the old name if the desired name causes troubles *)
if errnum is not -1700 then error "Error " & errnum & ": " & errMsg
end try
end repeat
end tell
on findReplace(theText, findString, replaceString)
(* replaces findString in theText by replaceString *)
try
set {OldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, findString}
set theText to text items of theText
set AppleScript's text item delimiters to replaceString
set theText to theText as text
set AppleScript's text item delimiters to OldDelims
return theText
on error errMsg number errnum (* in case of error: restore old text item delimiters *)
set AppleScript's text item delimiters to OldDelims
error "Error " & errnum & ": " & errMsg
end try
end findReplace
Copy the script, paste it in a new window in an AppleScript Editor and save it in Script format under an appropriate name in a convenient temporary place (such as the desktop or your downloads folder). Open DEVONThink, select ‘Open Scripts Folder’ in its Scripts menu and drag the new script from its convenient temporary place to DEVONThink’s ‘Scripts’ folder or one of its subfolders (how about the ‘Rename’ subfolder?). Return to DEVONThink, choose ‘Update Scripts Menu’ and the script is ready for use.
To use the script select one or more items in a DEVONThink database and run the script from DEVONThink’s Scripts menu. You will be asked for the text to be replaced (in my case: 'Long Feed Name: ’ without the quotes) and the replacement text (just click on OK to remove the text to be replaced).
Comments, criticisms and suggestions for improvement are welcome!