Fix missing indexed files using replicants of identically named imported files

For reasons that are embarrassing and beyond the scope of this discussion, I ended up with a significant number of missing indexed files in my database. Luckily, for each one of these files, an identically named imported file already exists within the database.

I would like to fix the issue with a script—namely, by replacing each missing indexed file with a replicant of its corresponding imported file (which, again, has the same name).

Here is an attempted script:

set logFilePath to (POSIX path of (path to desktop)) & "ReplacedMissingFilesLog.txt"
set logText to "=== DevonThink Missing Files Replacement Log ===" & return & return

tell application id "DNtp"
	set theDatabase to current database
	set missingRecords to (search "kind:missing") -- SHOULD get all missing indexed files
	
	repeat with theRecord in missingRecords
		set theName to name of theRecord
		set theSize to size of theRecord
		set theType to type of theRecord
		set parentGroup to parent of theRecord
		
		-- Build a search for candidates that are not missing and not indexed
		set candidates to (search "name:\"" & theName & "\" kind:document NOT kind:missing NOT indexed:true")
		
		set matchFound to false
		
		repeat with candidate in candidates
			if (size of candidate = theSize) and (type of candidate = theType) then
				-- Match found
				replicate record candidate to parentGroup
				delete record theRecord
				set matchFound to true
				
				set logText to logText & "✓ Replaced missing file: " & theName & return
				exit repeat
			end if
		end repeat
		
		if matchFound is false then
			set logText to logText & "⚠ No match found for: " & theName & return
		end if
	end repeat
end tell

-- Write log to file
do shell script "echo " & quoted form of logText & " > " & quoted form of logFilePath
display dialog "Replacement complete. Log saved to Desktop as 'ReplacedMissingFilesLog.txt'."

Obviously, line 6 is incorrect because kind:missing is not a valid search term in DEVONthink. I would be grateful for your help.

UPDATE—I think I found the answer: Find DT entries with missing files

Which AI did you use for the above script? Because it has several issues.

Here’s an old script that looks for missing files in the current database and then presents them in a main window:

-- Missing Files

tell application id "DNtp"
	set theDatabase to current database
	set theContents to contents of theDatabase
	set theCount to count of theContents
	set theName to name of theDatabase
	set theMissingRecords to {}
	
	show progress indicator "Checking " & theName & "..." steps theCount with cancel button
	
	repeat with i from 1 to theCount
		if cancelled progress then exit repeat
		set theRecord to item i of theContents
		set thePath to path of theRecord
		if thePath is not missing value and length of thePath > 0 then
			try
				set pathExists to false
				tell application "Finder" to if exists thePath as POSIX file then set pathExists to true
				if pathExists is false then copy theRecord to the end of theMissingRecords
			on error msg
				display dialog msg
			end try
		end if
		step progress indicator (name of theRecord) as string
	end repeat
	
	hide progress indicator
	
	if theMissingRecords is not {} then
		if exists main window 1 then
			set theWindow to main window 1
		else
			set theWindow to (open window for record (root of theDatabase))
		end if
		set search results of theWindow to theMissingRecords
	else
		log message theName info "No missing files found."
	end if
end tell

This is very helpful, thank you.

(I used ChatGPT 4-o and it didn’t go well.)

1 Like