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.