rename part of path

Foolishly having erased all my .doc files after importing all files into my database, I have had to bring back the trashed doc files (luckily backed up), and then reconnect my files that are in the DB with the newly restored .doc files that are in finder. Problem is, I had already rearranged them in the DB, and because of a reorganization of my “documents” finder folder, the restored doc’s were not where they were when they were imported into DT.

As such, I have had to do a search in DT for everything with a .doc ending, and then manually change the path of the file using the information bar.

With more than 1000 doc files, this is obviously taking forever. Looking in the scripts, I didn’t find the equivalent of a “replace text” in the paths menu. I dont’ want to rename the whole path (there is a script which does that), but rather just wanted to modify what is already there, a search and replace function.

Could anybody help? Thanks.

Here’s a script to find & replace strings in the path of selected items (including items inside groups & subgroups):


-- Rename Paths.
-- Created by Christian Grunenberg on Tue May 23 2006.
-- Copyright (c) 2006. All rights reserved.

tell application "DEVONthink Pro"
	activate
	try
		set this_selection to the selection
		if this_selection is {} then error "Please select some contents."
		
		repeat
			display dialog "Find:" default answer "" buttons {"Cancel", "OK"} default button 2
			set searchString to the text returned of the result
			if searchString is not "" then exit repeat
		end repeat
		
		display dialog "Replace:" default answer "" buttons {"Cancel", "OK"} default button 2
		set replacementString to the text returned of the result
		
		if searchString is not equal to replacementString then
			show progress indicator "Renaming Paths..."
			my renamePaths(this_selection, searchString, replacementString)
			hide progress indicator
		end if
	on error error_message number error_number
		hide progress indicator
		if the error_number is not -128 then
			try
				display alert "DEVONthink Pro" message error_message as warning
			on error number error_number
				if error_number is -1708 then display dialog error_message buttons {"OK"} default button 1
			end try
		end if
	end try
end tell

on renamePaths(theseRecords, theSearch, theReplacement)
	local this_record, this_path, this_type
	tell application "DEVONthink Pro"
		repeat with this_record in theseRecords
			set this_type to type of this_record
			set this_path to (path of this_record as string)
			if this_path is not "" then
				set the path of this_record to my replaceString(this_path, theSearch, theReplacement)
			end if
			if this_type is group or this_type is sheet then
				step progress indicator (name of this_record as string)
				my renamePaths(children of this_record, theSource, theDestination)
			end if
		end repeat
	end tell
end renamePaths

on replaceString(theString, theOriginalString, theNewString)
	set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, theOriginalString}
	set theStringParts to text items of theString
	if (count of theStringParts) is greater than 1 then
		set theString to text item 1 of theStringParts as string
		repeat with eachPart in items 2 thru -1 of theStringParts
			set theString to theString & theNewString & eachPart as string
		end repeat
	end if
	set AppleScript's text item delimiters to od
	return theString
end replaceString

Christian…excellent!