Smart rules: how to test if another document already exists?

I’m writing a smart rule that creates (via some AppleScript code) a document in a certain known group based on when a new document shows up in an indexed folder. Once the document is created, I don’t want the smart rule to run again. But, in a smart rule, how can I test for the existence of some document other than the one for which the smart rule is executing?

More concretely: suppose I have an indexed folder /SomeIndexedFolder and the smart rule’s purpose is to search in /SomeIndexedFolder and create new documents in /OtherFolder, triggered when certain kinds of documents (e.g., image files) show up in /SomeIndexedFolder. If a new document “doc” shows up (i.e., /SomeIndexedFolder/doc), it creates /OtherFolder/otherDoc. I need to write a condition that says “don’t create /OtherFolder/otherDoc if it already exists”. How can I do this with a smart rule condition?

(The name “otherDoc” is derived from a custom metadata field value in “doc”, if that makes any difference.)

1 Like

But, in a smart rule, how can I test for the existence of some document other than the one for which the smart rule is executing?

You can’t.
The matched document can trigger the smart rule, but you’d need to use an Execute Script to do the subsequent processing.

As usual, thank you for your super-fast reply.

What I’m doing now is testing for the condition in the script itself, and having the script return early if /OtherFolder/otherDoc exists – roughly like this:

property groupPath: "/OtherFolder"

on performSmartRule(selectedRecords)
	tell application id "DNtp"
		repeat with rec in selectedRecords
			set foo to get custom meta data for "foo" from rec
			if (exists record at groupPath & "/" & foo) then
				return
			end if
			... do stuff if it doesn't exist ...
		end repeat
	end tell
end performSmartRule

But, the smart rule always matches the same documents in /SomeIndexedFolder. Normally, I think that DEVONthink will only execute the smart rule on newly added documents in /SomeIndexedFolder and not all documents all the time (is that correct?). However, I have a problem: another one of my smart rules runs a perform smart rule ... trigger import event, which I think will cause this smart rule to trigger each time. That’s inefficient and will get increasingly worse as more documents match the rule. Hence, my need to find a condition that prevents the rule from matching.

Update: I’ve improved the situation by making this smart rule not trigger automatically on import, and instead, having the other smart rule (the one that runs perform smart rule ...) use an Apply Rule action to run this smart rule explicitly. Seems to be working, though this approach (which I’ve had to do for another recent situation) is indirect and unobvious. Perhaps a future DEVONthink update can provide some features to help in these situations.

No, that’s not correct. DEVONthink will continue acting on existing matching items in the specified location. That’s why it’s important to be specific in the criteria and applied actions to avoid reprocessing the same files over and over again.

1 Like

Thanks – that’s important to know.

You’re welcome.