Smart Rules and applying them

Hi there, I have set up a series of smart rules which will file my receipts in the correct groups. Previously used Hazel to do this. The receipt emails are converted to pdf as soon as they are imported which works well. I don’t seem to have the settings in the smart rules just right as I have to manually apply the rules. I was wondering if I could set up a shortcut key stroke to apply rules instead of having to through the action menu. Or maybe there is a better option in the smart rule itself.
Anyway, I’m open to suggestions !!
Thanks
Dorothy

Here’s an example of a rule I use for filing receipts:

2021-07-04_21-44-02

All you really need to do is to ensure:

  1. You set the rule to check the place where the original file is imported (in my case, the global inbox).
  2. You set Perform the following actions: to On Import. (You can also add On Demand, if you wish, by clicking on the plus sign.)

In my case that automates the filing of any receipt, named in accordance with the rule, which Hazel automatically puts in the DT global inbox.

I hope that’s reasonably clear but if it isn’t please don’t hesitate to say so!

Stephen

1 Like

Thanks Stephen
I am importing emails to the global inbox and on import they are converted to pdf by a smart rule which works really well. Then I have a smart rule say for Apple receipts now a pdf which renames them and will file them in the correct group/folder.
On demand means I have to activate the action menu and find apply rules. It was this step I’m trying to eliminate.
Dorothy

Dorothy, if I understand you correctly, your first smart rule always converts the e-mail to pdf; you then have further smart rules which would file the pdf appropriately, but those smart rules don’t trigger. Is that right?

If so, the reason is that when you import the e-mail, the on import trigger is activated. Following the conversion however, the on import trigger is not triggered a further time. Seeing as this is, in effect, a new document though, the initial trigger won’t trigger any further smart rules.

The basic solution is to add a script to the first smart rule which triggers the on import trigger on the converted document. The precise design of that script is somewhat dependent on your smart rule.

Basically it will be something like:

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			perform smart rule trigger import event record theRecord
		end repeat
	end tell
end performSmartRule

That script triggers the on import trigger - smart rules which use that trigger will now run on the document. The document needs to be appropriately defined though; if you post a screenshot of your first smart rule (i.e. the one which does the conversion), then I’ll adapt the script and post back here; I think (but need to test) that the conversion will need to take place in the script too, rather that as a smart rule action.

Again, this is all assuming I have understood the problem correctly.

Things to note:

  • your smart rule must not end with a Cancel action if you want further smart rules to run
  • smart rules are run in the order they appear in the side bar

PS If you are using the Convert action in your smart rule, it might be sufficient to use Convert & Continue instead; it would be worth checking to see whether subsequent smart rules then trigger.

PPS this post is too long :smiley: the short version is: pls post a screenshot of your smart rule, then I can experiment before providing an answer :smiley:

3 Likes

Dorothy, I suspect @Blanc’s sol;ution will be required if:

  1. you’re importing a variety of emails into the global inbox (all of which are presumably converted into pdfs); and
  2. only some are receipts which require filing accordigly.

If, on the other hand, all imported emails are receipts which need filing appropriately (i.e. in different groups) within DT I don’t currently understand why “move” rules can’t simply be appended to the “renaming” rule.

The answer, as @Blanc indicates, is, I think, to post screen shots (suitably redacted if necessary) of the smart rules.

Come what may, you’ll end up with some solution from soneone on the forum.

Stephen

1 Like

Hi there @Blanc many thanks for your message. You have understood my question absolutely correctly !

Here is my conversion smart rule
Image 05-07-2021 at 2.31 pm

and here is an example of the many rules I have for filing the receipt pdf’s created with the conversion rule.
Image 05-07-2021 at 2.33 pm

Look forward to hearing what you think
Thanks Dorothy

Thanks @Stephen_C
I have posted to screen shots so will be interested to see what @Blanc thinks of my efforts !
Dorothy

I’ll adapt the script and text it this evening and post it here

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.

Hi @Blanc Gosh, thank you so much for doing all of that for me ! Bear in mind I am new to DT and not a scripting person !! Although I can work out what the script does I am missing the basic piece of information which is where exactly should I put it !
When I edit the smart rule I don’t see anywhere where there is already a script to replace. Please be patient with me and explain how I should do this !
I didn’t realise that Move to database is only for indexed files so that is really helpful. I will alter all the other smart rules to the on import trigger.
This is such a great group and even in my old age I am learning all the time !

Thanks again
Dorothy

Hi, I have cracked it !! If I had read your message more carefully I would have understood what to do ! Ive found the existing script so I will copy and paste and keep you posted !!
Apologies !
Dorothy

No need to apologise - I’m sorry, I should have used a screenshot which would better have demonstrated what I was pointing to.

I learned what little scripting I am proficient in with the aid of the great ppl on this forum - I happy to share. Let me know whether things work as wanted & expected :slight_smile:

Hi there, I copied the script and added it to my smart rule. I have tried it out but…
The rule has picked up the .eml file as there is a 1 in the smart rule title but it hasn’t done anything with it. The file is still in the inbox.
Did I need to add anything to the script or just a straightforward copy and paste ?

Dorothy

If the file was already in the inbox, then you will need to trigger the rule manually for the rule to act on that file. It should, however, run automatically on any file which arrives in your inbox.

You trigger the smart rule manually by selecting the rule in the side bar and selecting “apply rule” from the context menu.

I used DT to look at my emails and imported the receipt email.
I ran the smart rule with the script manually and I got the pdf in the inbox and 1 in the M&S smart rule title
Had to manually run the M&S smart rule to get it filed even although I had converted that rule to be on import

I need to think about that; is the m&s rule listed below the initial rule in the sidebar, or above it?

The rule worked in my basic tests; I’ll try and recreate the exact conditions you are using tomorrow and see why it’s not working as expected. I would have expected both the initial rule and the m&s rule to trigger automatically when you imported the mail. Are there any rules listed above those two rules which may be acting on those files and which contain a Cancel action?

Both smart rules are way down the list ! I will re-organise the list and try again but a bit later, some chores to do !! Here is a screen shot of my rules list !

Dorothy

Dorothy, maybe I should not interfere again, having deferred to Blanc’s skills, however I just wanted to make one comment. I, too, file a lot of receipts but I don’t bother to identify specific receipts at the time I file them (they’re simply filed by year). I then rely on DT’s powerful search facility to find what I want.

I appreciate that may well not suit you but, for me, it’s a simpler approach which works well.

Stephen

4 Likes

Whilst I’m aware that you are practicing British modesty, I still feel the need to point out that - as far as I am concerned, Stephen - your input into this forum is universally valuable and that you have repeatedly posted on topics on which I have no knowledge :slight_smile: Together we are all strong.

@Dorothy please try moving the .eml to .pdf rule to the top to see whether it then runs automatically on import. I’m going to experiment a little (I would expect the script’s trigger the import trigger to start going through the rules from the top; as such, I’m not sure why - even when manually triggered - the rule doesn’t cause your second rule to automatically trigger. There are two possibilities: a rule further up is also handling the document and contains a cancel action; there is - what I believe is - a bug in DT which causes some rules not to trigger automatically, unless the search term in is enclosed in "; as such, you might try “M&S” rather than M&S. Also you might try a receipt which triggers any one of the other rules, just to see.)

If it still doesn’t work, please post a screenshot of the (new) initial .eml to .pdf rule which you are now using (PS it does now do OCR, I included that in the script; it just didn’t previously).

1 Like

That’s a very nice comment @Blanc: thanks (modestly :grin:)

Stephen

3 Likes