Background re-OCRing of defective PDFs

Thanks both. Here’s the solution I arrived at, using Shane Stanley’s FileManagerLib. (N.B. The compressPDF handler compresses the file with PDF Squeezer, as mentioned here.)

use AppleScript version "2.8"
use scripting additions
use myLib : script "myLib"
use script "FileManagerLib" version "2.3.5"

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with eachRecord in theRecords
			try
				set oldPath to path of eachRecord
				set newRecord to ocr file oldPath type PDF document to incoming group with waiting for reply
				set newPath to path of newRecord
				---
				set oldPathElements to parse object oldPath
				set newPathElements to parse object newPath
				if (full_name of newPathElements) is not (full_name of oldPathElements) then
					rename object newPath to name (full_name of oldPathElements)
				end if
				move object newPath to folder (parent_folder_path of oldPathElements) with replacing
				delete record newRecord
				---
				set theResult to myLib's compressPDF(oldPath, "l")
				if theResult is "" then add custom meta data 1 for "mdfilecompressed" to eachRecord
			on error theError
				display alert theError
				return
			end try
		end repeat
	end tell
end performSmartRule