Smart Rule with event "Before Synchronization" doesn't work as expected

Hi, recently I made two Smart Rule (scripts) that closed all databases that were opened due to syncing:

  • One that writes all open database names into a text file
    Event: Before Synchronization

  • One that closes all databases whose name is not included in the text file
    Event: After Synchronization

This worked fine while testing and also in practice. However now it doesn’t work at all anymore. Already restarted DTTG, DEVONthink, iPhone and Mac.

After spending far too much time with this I tested how often a Smart Rule with event Before Synchronization is actually triggered:

  • with 5 synced databases (including the global inbox) the below script reveals that the Smart Rule is triggered 10 times.

What’s that?

-- Write count of Smart Rule executions into plist

property thePlist_Path : "/Users/USER/Desktop/Triggered_Count.plist"

on performSmartRule()
	tell application "System Events"
		try
			
			try
				exists POSIX file ((thePlist_Path) as string) as alias
				
				tell property list file (thePlist_Path as string)
					set theTriggered_Count to value of property list item "Triggered_Count"
					set theTriggered_Count_new to theTriggered_Count + 1
					set value of property list item "Triggered_Count" to theTriggered_Count_new
				end tell
				
			on error
				
				try
					make new property list file with properties {name:(thePlist_Path) as string}
					
					tell property list file (thePlist_Path as string)
						set theTriggered_Count to 0
						make new property list item at end of property list items of contents with properties {kind:integer, name:"Triggered_Count", value:theTriggered_Count}
						set theTriggered_Count_new to theTriggered_Count + 1
						set value of property list item "Triggered_Count" to theTriggered_Count_new
					end tell
					
				on error error_message number error_number
					if the error_number is not -128 then display alert "System Events" message error_message as warning
					return
				end try
			end try
			
		on error error_message number error_number
			if the error_number is not -128 then display alert "System Events" message error_message as warning
			return
		end try
	end tell
end performSmartRule



But the main problem in case of my initial Smart Rules seems to be that now event Before Synchronization isn’t triggered before the sync anymore. Writing all opened databases’ names into the text file used to include all open databases before a given synced database was opened (i.e. the synced database name didn’t show up it the text file). Now synced databases are included. As I wrote the scripts after the last DEVONthink update I guess it could be a race condition.

Do you use two sync locations?

No, just Bonjour (Mac <-> iPhone).

Did you manually sync? Otherwise you might have counted multiple syncs.

Sure, manually, Created a dummy document in one database on iPhone, then synced. Tried several times, including restarting iPhone and Mac.

Before I used this test script I really tried everything to make the initial Smart Rules work again, without luck. The scripts worked when I wrote them, they also worked in practice, but now the database that’s currently synced is always included in name of databases. It really might be a race condition that didn’t catched me before. It’s weird.

But even a race condition would probably not explain why event Before Synchronization triggers twice. First thought I messed something up in the test script’s logic, however I couldn’t spot such user error.

Why shouldn’t it be included?

That’s something I can’t answer :wink: However it was not included before. So there’s at least an inconsistence (or a race condition?).

While writing the scripts the currently synced database was not included, i.e. the Smart Rule was triggered before the database was opened (or at least it handled name of databases as if the currently synced database were not open yet).

What exactly happens when one uses the event Before Synchronization?

Did you try the test script?

Just synchronized 4 databases (one after another manually, preference is also set to manually, only one sync location) and the count is 4.

In general I sync one database at a time (i.e. start the sync from within a database). So not sycing all databases (via DTTG’s “home screen”) was the first thing I thought of after the Smart Rules failed. But it doesn’t make a difference whether a sync is started for one or all databases.

Criss, this isn’t important, not at all. Would be great if it would work again, but I’ll also happily close databases manually after syncing. It’s up to you to decide whether that needs further investigation from my side. I’ll make tests if it might help. For now, all I can say is that the scripts worked and stopped working, And the posted test script shows 10 executions for 5 synced databases. I’ve double checked this before posting.

DEVONthink To Go? Only the local sync operations of the Mac are actually relevant for the smart rule.

A screenshot of the used sync setup for the test might be useful, thanks. BTW: Is it possible that one of your other smart rules triggers this rule a second time, e.g via the perform smart rule command or the Apply Rule action?