Smart Rules and applying them

Now then… :slight_smile:

A couple of points: the first smart rule is called .eml to .pdf with OCR but actually doesn’t - to the best of my knowledge - perform OCR. It would convert an image file to PDF, but would not turn the contents into text. Obviously that would not be a problem for files which already contain text (e.g. many e-mails).

And the problem is as I had guessed: the new file being produced by the rule does not trigger the appropriate triggers embedded in the subsequent rules.

We can solve both problems by changing the actions in your first rule to:

Execute Script - Embedded

The script (which you need to paste into the window which opens when you click edit script, overwriting the standard script which is already there) is:

on performSmartRule(theRecords)
	tell application id "DNtp"
		try
			# show the progress indicator and allow you to cancel if required
			show progress indicator "OCR" cancel button 1 steps count of theRecords
			repeat with theRecord in theRecords
				if type of theRecord is PDF document then error "Adapt your smart rule to exclude PDF. Process failed to avoid looping."
				# cancel if you click the X in the progress indicator
				if cancelled progress then error number -128
				step progress indicator (name of theRecord) as string
				# set the requisite variables
				set theName to name of theRecord
				set theDatabase to database of theRecord
				# perform conversion to PDF and move the original record to trash
				set thePDF to convert record theRecord to PDF document
				move record theRecord to trash group of theDatabase
				# check to see whether OCR needs to be performed
				if word count of thePDF is 0 then
					# do OCR and delete the intermediary record
					set theFile to path of thePDF
					set newFile to ocr file theFile to parent 1 of thePDF type PDF document
					set name of newFile to theName
					delete record thePDF
					# tell DT to treat the document as if it had just been imported
					perform smart rule trigger import event record newFile
				else
					perform smart rule trigger import event record thePDF
				end if
			end repeat
			# handle any errors
		on error error_message number error_number
			hide progress indicator
			if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		end try
		hide progress indicator
	end tell
end performSmartRule

The script should be the only action taken by your initial smart rule. It is important that your first smart rule should limit the actions to documents which are not PDF (which is what you are doing by saying the the document must be an E-Mail), otherwise a loop will occur (actually it won’t, because I’ve built in a failsafe - you’d receive an error message).

Re your second rule: You will need to add the “on import” trigger; whether any of the other triggers are required is up to you and how else such documents might arrive in your inbox. The Move into Database action is only needed for indexed files; as far as I can tell from what you have written, you aren’t indexing and can forgo that action.

I have tested the script and found it to do as I expected. Please test for yourself, ideally with files which you can recreate. Let me know how you fare.

1 Like