Regular Expressions and Smart Rules v3.5

How specifically do we use regular expressions? I am trying to see if I can have DT do what Hazel does:

  • Match PDF file for content
  • Rename based on data from the PDF. (iCloud Subscription - May 15, 2020)

1 Like

What are you expecting to find?

Invoice date inside the document. For example: May 15, 2020

As noted in the Help > Documentation > Appendix > Smart Rule Events and Actions…

It is best to start off simply with an action that can be used for debugging, like Display Alert. That was you can more easily test if you’re getting the expected results or not. Then add subsequent actions as needed.

1 Like

@BLUEFROG

Thank you. It would help if a screenshot that you posted was included in the manual as it makes a lot more sense.

I got excited for a moment thinking that I can have DT scan for total $ value in a receipt and then change the custom metadata “total” to the $ found inside text. However, I see that I cannot set custom metadata to the RegEx result. :frowning:

I assume that this can be done with an Apple Script?

Workflow example:

For each document do:

  1. Scan text for RegEx (Grand\sTotal:CDN$\s)([0-9]+.[0-9]+)
  2. Set custom metadata total to \2 (second result of the RegEx)

Problem is I don’t know how to write an Apple Script.

Please advise.

Figured it out. DT Already includes an Apple Script inside Smart Rules folder. The script is called “Assign Document Date & Amount.”

  1. I made a copy and named it Assign Document Total. Total includes the price with tax.

  2. Script Content:

-- Assign Amount

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			try
				set theAmount to document amount of theRecord
				add custom meta data theAmount for "Total" to theRecord
			end try
		end repeat
	end tell
end performSmartRule

Result:

Note, this wouldn’t work…

Why not?

Because the Amount parameter doesn’t accept regular expressions. It’s returning nothing and the Document Total is only getting added by the Execute Script action.

Doh! What would be a proper Apple Script to include Scan Text for Amount inside of the script?

I would like to select several PDF files and have DT Apple Script Find and Set the Total to each one.

-- Assign Amount

try
	tell application id "DNtp"
		set this_selection to the selection
		if this_selection is {} then error "this_selection select some contents."
		repeat with this_selection in theRecords
			try
				set theAmount to document amount of this_selection
				add custom meta data theAmount for "Total" to this_selection
			end try
		end repeat
	end tell
end try

What was the result of the script you just posted?

Nothing :frowning:

Does this include scanning the document for the amount?

set theAmount to document amount of this_selection

Yes, this property (and the same placeholder in smart rules) scan the text of the document. Could you send a copy to cgrunenberg - at - devon-technologies.com? Thanks!

@cgrunenberg, is it possible to use the regex capture group (from the smart-rule) in an applescript (in the same smart-rule)?

No, since there are no variables and data isn’t passed between actions in a smart rule.

2 Likes

Great, nice work @cgrunenberg and the crew! This eases my script workaround.

Any details about the regular expression ‘dialect’ that is used? Is it ICU?

@BLUEFROG

Any more thoughts on the script to make it work? Does it work on your end?

In this state it returned a value, though it was actually not the correct amount.

tell application id "DNtp"
	repeat with this_selection in (selection as list)
		set theAmount to document amount of this_selection
		add custom meta data theAmount for "Total" to this_selection
	end repeat
end tell

Don’t use try… end try blocks while you’re debugging. It masks errors in the code.

1 Like

Thank you. It works now. Although a hit and miss for proper number.

No problem and remember, it’s only a computer :wink:

1 Like