Removing a part of the name of a number of entries

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!

I modified the Rename using RegEx script to accept an empty replacement string:

Re: Document name renaming

RFE for Dtech: Integrate this change into the master version.

Great addition to DEVONthink Pro Office, works fine here. Up to now I did this with NameChanger, which means export and reimport and sometimes it’s just three files and, well, you know the rest. Dankeschön!

(For a moment I thought if it would be cool, if it picked the part to replace from the clipboard and suggest it, but pasting from the clipboard is just so quick.)

I had in mind to change that script next time I need it. Great that there is already a solution, probably saves me a lot of work. Thanks!

I just read how sjk solved the problem with an empty replacement string in the Rename using RegEx script by simply removing a test for that string being not empty. :smiley:

In the case of the Replace Text in Names script the problem is not caused by such a condition in the script, but by DEVONThink’s display name editor refusal to accept an empty string. I suspect this is a bug, for the script has code to prevent the user from entering an empty string to be replaced (which doesn’t make sense if the command itself does so already).

Glad you find my script useful, berndm!

Actually, I think it is a good idea to pre-fill the dialog that asks for the text to be replaced with the text from the clipboard.

I haven’t test this yet but I think that replacing:


   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

by:


    set toReplace to get the clipboard
    set toReplace to display dialog "Enter text to replace: " default answer toReplace
    if toReplace = "" then
	repeat until toReplace is not ""
            set toReplace to display dialog "Enter text to replace: " default answer toReplace
	end repeat
end if

might do the trick.

Problems might occur, though, when the clipboard contains a picture. I’ll try to find this out (but it might take a few days before I have time).

Try


set toReplace to «class ktxt» of ((the clipboard as text) as record)

This produces a funny kind of ‘loop’:

If I replace “1234” with “abcd” the result is “1abcde2abcde3abcde4”.
If I replace “12345” with “abcde” the result is “1abcde2abcde3abcde4abcde5”
and so on.

This I couldn’t get to work, but maybe just due to my shortness with AppleScript.

You enter «class ktxt» into the editor by pressing [b]option-[/b] and [b]shift-option-[/b] for the left and right angle brackets, respectively