What logic applies to a "Daily" smart rule?

I have a Smart Rule which runs daily. Today I noticed it hadn’t ran in three days, while I had DT open during those days. What is the logic for a “Daily” rule? E.g. does it run as soon as DT opens on a day it has not run yet? Or does it run on a random moment per day? And what happens when a rule errors? Is it tried again before the next day?

1 Like

Great Question!!!

What is the rule? A screencap could be useful.
And is the database it’s acting on open?

At least once every 24 hours. Depending on the availability (e.g. Mac sleeping, DEVONthink not running or databases closed) this might be delayed though.

Pretty standard rule:

Interesting question about the database being open - I thought it was, but maybe with closing/opening it was closed for a moment. I’ve now set it to Hourly and will see what happens.

Regarding the DB being open: I actually had to include some checks in my scripts to prevent it from running when the DB wasn’t open. Could it be that Smart Rules trigger when a database is not fully opened yet? This mail database takes a few moments to open and I get an error from my script sometimes when it tries to run and the DB isn’t open (which seems weird, because rules can only be active when the DB is open, right?)

A database can be only closed or opened. But I’m not sure right now whether the search scope is actually relevant without any conditions.

Did you notice whether you had any entry in the log? I regularly see scripts time out (I can’t remember the error message off hand - it’s Apple event timed out or something of the kind) when they are executed by smart rules when I am away from the Mac. So what I’m saying is that the smart rule may well be running, but the script itself fails to run. Having executed once, the smart rule would then settle back down at the fire place with a cup of tea…

The source of these scripts would be useful, maybe they just need a longer timeout than the default one?

It’s my auto-import mail rule - it’s a bit complex to workaround the DT3.8.3 smart rule limitations. I’ve now included a with timeout section to check if that solves it:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

property pythonCmd : "/Users/mdbraber/.pyenv/shims/python3"
property pythonScriptPath : "/Users/mdbraber/iCloud Drive/Coding/Devonthink import mail/"
property pythonScriptName : "import-mail-to-dt.py"
property importCmd : pythonCmd & " " & quoted form of (pythonScriptPath & pythonScriptName)
property replaceScript : load script POSIX file "/Users/mdbraber/Library/Application Scripts/com.devon-technologies.think3/Smart Rules/Replace mail attachments.scpt"

on run
	tell application id "DNtp" to my performSmartRule(selection as list)
end run

on performSmartRule(theRecords)
	with timeout of 3600 seconds
		tell application id "DNtp"
				-- Check if we the datase is open, because sometimes when starting
				-- DT it isn't open yet while the smart rule already runs (which is
				-- weird but might have to do with the opening taking longer?)
				set theDatabase to database "Mail 2020-now"
				show progress indicator "Importing new mail…"
				set theOutput to do shell script importCmd
				-- This is a workaround because 'perform smart rule' doesn't work
				-- from smart rules in DT 3.8.3 so we're including our script and 
				-- using tagged records. Ugly, but it works for now. This should
				-- be fixed in DT 3.8.4
				set theRecords to search "tags:import scope:\"Mail 2020-now\""
				-- This script replaces attachments and also strips the import tag
				-- (for now)
				replaceScript's performSmartRule(theRecords)
				hide progress indicator
			on error error_message number error_number
				hide progress indicator
				if error_number is not -128 then log message error_message
			end try
		end tell
	end timeout
end performSmartRule