Trouble executing external script attached to a reminder

I have a script that I want to run at a designated time each morning. I don’t know how to do this best, so I am trying to attach it as an external script to run as a reminder. The script itself runs fine when it is not attached to the reminder, but I cannot get the script to run when I attach it to the reminder.

As a simple test, I am trying to execute a simple script once:
Screenshot 2023-06-03 at 11.10.22 AM

on performReminder(theRecord)
	tell application id "DNtp"
		display alert "it works"
	end tell
end performReminder

But I cannot even get this to work. The designated time comes and nothing happens.

Am I doing something wrong? The script should run at the reminder time, correct? Is there any additional setting I need to turn on?

Are reminder alarms enabled in the General preferences?

Yes, they are

Just tried this script successfully using a new document and a reminder one minute later. Is it possible that other, slow scripts are already executed at the same time?

Ok, after restarting multiple times, I was able to get the simple reminder to execute the script above. However, now I cannot get it to run my actual intended script, which just applies a smart rule to all of the tags in open databases. The script runs on its own just fine, but when it runs attached to the reminder, it just beeps and then all of my smart rules become greyed out and can’t be used until I restart DT3. The smart rule that is being called in the script is working fine before the script is run. Is there any way around this?

The script attached to the reminder is:

on performReminder(theRecord)
	
	script R
		property plistPath : "~/Library/Application Scripts/com.devon-technologies.think3/Menu/My Scripts/FileByTag4.plist" -- hold FileLocations Matrix
		--- These need to match the properties set in FilebyTags_SETUP_vXX
		property pkeyName : "PKEY"
		property highlevelTags : "/Tags"
		
		-- getDataFrPlist() retrive PrefixList matrix from file, used in Main.
		on getDataFrPlist(pname, plistPath)
			tell application "System Events"
				tell property list file plistPath
					try
						return value of property list item (pname)
					on error
						return value of property list item ("blank_blank")
					end try
				end tell
			end tell
		end getDataFrPlist
		
		
	end script
	
	tell application id "DNtp"
		try
			-- Read in pkeys
			set pkey to R's getDataFrPlist(R's pkeyName, R's plistPath)
			-- read in Database Names
			set DBNames to R's getDataFrPlist(databaseNames of pkey, R's plistPath)
			
			set openDBs to name of every database
			
			repeat with i from 1 to length of DBNames
				set DBN to DBNames's item i
				if DBN is in openDBs then
					set tagsRec to get record at R's highlevelTags in database DBN
					set theQuery to "Kind:Tag"
					set theResults to search theQuery in tagsRec with exclude subgroups
					if length of theResults > 0 then
						repeat with j from 1 to length of theResults
							perform smart rule name "FileByTags" record theResults's item j
						end repeat
					end if
					log message ("Ran FileByTags on " & length of theResults as string) & " tags in database " & DBN
				end if
			end repeat
			
			
		on error error_message number error_number
			if the error_number is not -128 then display alert "Err: Main; DEVONthink" message error_message as warning
		end try
	end tell
	
end performReminder

Note that the reason I am trying to run this script on a regular basis is that I was having trouble performing smart rules inside scripts attached to other smart rules. The same thing would happen – all of my smart rules would become greyed out and unusable until I restarted DT3. It seems like this should be possible to do if the smart rules are run sequentially (i.e., if I have multiple smart rules set to run on the same trigger) – however, I am not sure if that is the case. Any guidance would be appreciated. Thanks.

Can you run the script in Script Editor without errors? Might help to remove the try block for this. Also, I’d simply add the functions performed by your smart rule to this script inside the repeat loop. The whole thing looks awfully complicated to me – what is it supposed to do?

Yes, the script runs in script editor just fine. To run it in script editor, I only add the line to the top:

tell application id "DNtp" to my performReminder() 

and it runs as a standalone script with no issues. I cannot just replace the Perform Smart Rule line with the functions performed in the smart rule because the smart rule script is quite complex and long – however, it works just fine as a smart rule and successfully runs whenever it is triggered. I don’t understand why I am not able to just perform the smart rule within this script when the smart rule works just fine outside of the script.

With respect to this being complicated, I am open to suggestions to make it less complicated. What I need is to periodically apply a smart rule (which works fine on its own) to everything in the /Tags group. I am open to whatever is the easiest way to do this. Is there some reason why this method shouldn’t work?

Why not set a periodical trigger for that rule?

That’s something @cgrunenberg might know.

To me, it looks over-engineered. Why have constants in a property file, why set the same query in a repeat loop, why check for an empty list if the repeat loop won’t run anyway in that case? Basically, you have a list of databases you want to handle, and some of those have to be open (why? What happens if one of them is closed?). Then you access their Tags group and loop over all of its top-level entries, i.e. tags proper. Applying this complicated smart rule to each tag in turn…

Good idea, but I wanted to run the smart rule periodically on every item in the Tags group of every open database (and not on every tag in every database). I want to avoid having to create a separate smart rule for every possible database. I thought I had solved it by having the smart rule run periodically on every ordinary tag, but there are group tags that remain in the Tags group, which I am still figuring out. There are many group tags in the database that aren’t in the Tags group that I don’t want to run on. In short, I am not sure how to make a smart rule apply to every item in every Tags group of every open database.

I would attach my script above to its own smart rule, but I have been running into the issue with the smart rules greying out and becoming non-functional when I try to perform a smart rule in a script that is called by another smart rule. It doesn’t seem to happen all the time, but I haven’t been able to pinpoint when it is ok to do this and when it breaks things.

Scripts don’t run concurrently so there is nothing unusual from what I see ifyou are running scripts that run scripts.

Exactly this line without specifying a parameter?

Again, it’s not recommended to perform smart rules inside scripts that might call other scripts. Directly call the script instead.