Smart rule with the ability to include words marked with highlighting in the file name


what I’m up to:

Invoices I receive, whether by email or post, are stored in a folder monitored by Hazel as a PDF (paper is of course scanned beforehand). For recurring invoices, Hazel already has rules that automatically name the PDF files.

For other invoices (e.g. from a one-off online mailing) Hazel will only recognise the date.

When Hazel has completed its tasks, the PDF files are automatically sent to the Devonthink inbox - folder with the file name consisting of “date” and “invoice”.

So far everything works.

I’m thinking of using the PDF Highlighter to highlight the words in the “rare” invoices (just like with a highlighter) that are to be included in the file name later. The highlighting of the words (e.g. product and company) would be done by hand, which can be done during the Hazel actions or later in Devonthink.

What I would probably need would be a script that transfers the highlighted / marked words into the file name. This should result in a file name consisting of date, company and product. E.g. 2020-06-10 Iron Karl T-beam.pdf

I saw that Devonthink has a script that allows you to save temporarily marked text as Finder comments to the PDF. Combined with a smart rule and the action “Change Name”, the Finder Comment can be used as file name. (Tried it - works)

Is such a thing also possible with highlights? Devonthink displays the annotations with the highlighted text in the info bar. Only because I can’t write such a script myself, but only use it, I lack the knowledge to create such a script.

Is that even possible?

Translated with DeepL

Scripting highlighted text of PDF documents isn’t supported by DEVONthink, therefore this would probably require a scriptable third-party app.

Where did you see this script?

Oh, sorry, my bad.

It’s an intelligent rule. Within it, the Devonthink script “Comment” - “Add selected text” is executed, which saves the text temporarily selected by mouse as Finder comment in the PDF file. Afterwards, the “Change Name” rule and the placeholder “Finder Comment” are used to save the text previously saved as “Finder Comment” as file name.

Here is a screenshot

By the way a question: A double click on a rule to be executed manually always starts the edit mode. Is there a setting in Devonthink that if you double-click on the rule, it will be executed.

Translated with (free version)

The selected text can indeed be accessed via AppleScript but not highlighted text. In addition, what’s the purpose of the smart rule if it requires the current selection? Ideally smart rules should not depend on user input but run silently in the background.

No, this opens always the editor.

For most documents, I have already created rules with Hazel and Devonthink that handle the automated filing and saving of PDF files.
The action with the Finder Comment should only semi-automatically handle incoming invoices for which there are no fully automated rules.
Such invoices will be saved as PDF files in a folder monitored by Hazel. Hazel renames the file using the invoice date and the word “invoice”.
Renamed in this way, the file automatically ends up in the Devonthink inbox folder. A rule detects the word “Invoice” in the file name and moves the file to the current Invoices folder.
In these invoices, I then search manually for the text that will later replace the file name. The text is marked. Then the rule is started manually, which converts the marked text into a Finder comment. The rule uses the action “Change name” with the placeholders “sortable file date” and “Finder comment” for this.
This may sound a bit complicated, but it’s still reasonably practical.
As I already explained, the names of the files consist of the date, the name of the delivery company and the name of the goods.
This can only be automated for invoices from companies where I often place orders.
For companies that receive a one-time order, there can be no fully automated process for my way of using file names.
But I am always receptive to suggestions for improvement.

Translated with

In such cases I’d recommend a script, e.g. accessible via the Scripts menu, via shortcuts and also via the toolbar (if it’s in the toolbar folder) as the rule is manually executed and requires user input. Rules are ideal for automated and faceless operations.