How to convert month.year to date via smart rule?

Dear,

I have several salary statements that I want to classify with a smart rule. On those statements I have a fiels which states the month and year for which the statement is valid - like “04.23/1” for April 2023 (where the /1 means that it is the first statement for this month).

What I would like to do is to take the “04.23” and to convert it to (in this case) 2023-04-01 (year - month - 01).
How can I achieve this via a smart rule so that I can pickup this date to the file name and a custom metadata field?

Thank you for your support
Regards

Connor

Is the format of the dates you’d like to change always the same (like in your example) or are there likely to be several different formats floating about?

It’s worth having a read of this thread as it takes you half way to the answer: How to run a search on different date formats - #2 by cgrunenberg

1 Like

Thank you for your input. The format will stay as described. And tank you as well for the link - I will check it out.

Regards

Connor

You could scan the text for a regular expression like (\d\d\)\.([2-9]\d)\/\d and then work with the capturing groups \1 and \2 for month and year.
The RE matches

  • two digits (1st capturing group)
  • a literal dot
  • two digits with the first one being at least 2 (2nd capturing group)
  • a literal slash followed by a digit

@chrillek
Reads good and like exactly what I am looking for - and sorry for asking dumb questions, but how do I form a date out of this?
I understand that I can define a string looking like a date - but can I set an actual date out of this information?

Regards

Connor

Good point. The answer is “In principle, you can”. However, not what the smart rules offer directly. A minimalistic script does the trick, though:

const nameOfCustomMetaDataField = "spieldatum"; /* all lower case */
function performsmartrule(records) {
  const app = Application("DEVONthink 3");
  records.forEach(r => {
     const match = r.plainText().match(/(\d\d)\.([2-9]\d)\/\d/);
     if (match) {
       const month = match[1];
       const year = match[2];
       const newDate = new Date(+year + 2000, month-1, 1);
       app.addCustomMetaData(newDate, {to: r, for: `md${nameOfCustomMetaDataField}`});
     }
  })
}

This can be installed as an internal JavaScript script into a smart rule that selects the desired record types (markdown, OCRd PDF, simple text – perhaps also RTF). It searches for the regular expression, and if found, creates a new Date object as the first day of the month and year found (months are zero-based in JavaScript, therefore the subtraction of 1). Finally, this date is added to the custom metadata field, whose name is defined in the constant nameOfCustomMetaDataField in the first line.

Aside: The current functionality of smart rules seems to be lacking in this context: One can set a custom metadata field of type Date/Time only to one of the predefined date values that already exist as property of the record anyway. An optional string input would be a nice add-on, I think, foregoing the need to script here. @cgrunenberg?

@chrillek : thank you for your effort, I will have a look into it.

Interesting point of view - however, to put some more demand into it, it would be great to have the possibility to define and fill „own“ variables based on collected data and manipulation of this data. My comparison is the variable section of Microsoft Power Automate including it‘s scripting functionality. So maybe… on the long run… @cgrunenberg will be able to implement something like this?

I don’t quite follow: The script shows that you can already do what you’re asking for. And you’re, of course, not limited to smart rules, as you can run a script completely independent of a rule.

I was explicitly talking about the limitations with custom metadata in the action part of a smart rule. It would be nice to lift them, but it’s not strictly necessary as the desired outcome can easily be achieved with a script.

Not quite - or at least not according to my understanding.
In DT, I can collect data and a limited amount of scanned content from a file. What I cannot do (at least not from what I understood so far) is to define a new variable inside the smart rule without “real scripting”.

This is compared to my experiences in Power Automate, where you can very easily (and with quite easy defined functions) define a variable and manipulate it throughout the whole flow to finally represent what you want to have.

In this example, I would have created a new variable like

varReadDate=((year(20+\2),month(\1),day(01)),'yyyy/MM/dd') (not tested yet, but in this manner)

and refererred only to the varReadDate variable afterwards.

Just not to be mistaken - this is all the “nice” path. From my perspective, DT already offers a lot. This is my wish for simply improving the possibilities from my perspective :wink:

You probably mean “custom metadata”, and then you’re right: You can’t define them in a smart rule. “Variables” do not exist in DT, though.

That may well be the case. I don’t know that program. But what’s the point in this comparison? I can also define variables and functions in Emacs or in Visual Studio Code or in a ton of other programs. And I can do that with DT using either JavaScript or AppleScript. So

  1. What would be the difference then between “Power Automate” and DEVONthink, if both allow to define variables and functions?
  2. Why would DT provide the same terminology, syntax, and semantic as a probably very different program on another platform?
  3. Or are you stressing the “very easily” part? It took me about 5 minutes to write the script above. Is that “easy” enough?

As I did in the script. Again: Where’s the difference? Variables do not exist outside a programming environment. DT is not such an environment; therefore it doesn’t have variables. But it can be talked to from a programming environment, namely JavaScript/JXA or AppleScript. You can even call scripts written in these languages from inside DT.

So, if that suits you, you can write

and there you are. Well, it’s not a variable (why would it be, if the value is not going to change?) and it’s a Date object, not a string (as seemingly in your example). Which seems the natural choice for a date.

I suggest that you familiarize yourself with what DT can do and how it does what it does. In particular, you should check out the chapter “Automation” in the manual. There’s very little point in my opinion to comparing one program with one set of functions to another one with another set of functions.

The point of the comparison is that for me (with my possibly incomplete understanding) the smart rules in DT and a flow in Power Automate offer a way to add metadata (and custom metadata) to a file or data element. So for me they both serve the same aim - and therefore I find it valid to compare them to highlight the relevant advantages and disadvantages. No more no less - like you don’t know Power Automate, I do not know Emacs.

It’s not about providing the identical terminology, syntax or semantics. I just wanted to point out that it allows a very flexible approach with data. And that on a low-code base. And concerning the “very easy” part - so far I do not need AppleScript or any of the named alternatives in my professional or personal life. This is why it would take me significantly more than 5 minutes. So, as you see, it always depends on where you come from.

As said, a smart rule handles multiple elements - so therefore, in the smart rule itself, it’s a variable, as it changes from cycle to cycle.

Of course I will. However - just as my last statement, my point is not to compare the features itself, but just the approaches.

I suggest that you first think about what you want to achieve. My impression (which may of course be wrong) is that you tend to duplicate information into custom metadata. And that is, in my opinion, not a good idea.

For example, you have your statements with a month/date in the content. Now you look for a way to duplicate the same information into a custom metadata field – but why? It’s not getting any different or enriched, it just gets to another place. Personally, I’d put that information in the record name so that I can easily sort the statements.

And custom metadata fields are the same for all your databases. So, if you add a “statement date” field to your preferences, you’ll have this field everywhere, even in your invoices. Is it sensible to potentially create a lot of these fields that are only needed in some cases and then they replicate information that is already there?

Asked differently: What are you going to do with the custom metadata fields? One usage could be the amount field to store invoice amounts so that you can easily create a yearly/quarterly/monthly report.

That’s why I was putting “easy” in quotes – there’s no comprehensive concept of that. It all depends on your experience. Whatever you did with Power Automate will not be possible on the Mac, so you have to learn something new if you want to automate Be it Automator (on life support), JXA or AppleScript.

Another way is to sort the data by date and if you have a lot of forms with the same date using Keyboard Maestro allows you to at least provide some keystroke automation if you only have reasonable number of documents. ie: you can for example once a record is chosen use a KEYSTROKE to add the 2023-12-22 to the front of the name then a space then have the cursor sit there to wait for input.

If you have such a keystroke made then whenever you input or scan into DT3 … you can keep it going with every new inputed form. I do that daily but I am the only user of my DT3 and it is really quick to do and I am a neophyte on KM.

I don’t know if recommending a different app is appropriate, but I think you should try Noodlesoft’s Hazel. It’s a wonderful tool that can look for different types of data in the contents of a file and do exactly what you need. I actually have a rule that I think achieves what you are looking for. It can search for a date in a particular format inside the file, then rename it with that date in the custom format of your choice, add that to the comment section of the file too.

2 Likes

Thank you for tossing in Hazel. I used Hazel a lot in the past. Due to a change in my setup, I get my documents now into DT directly.
Therefore, I would like to keep the files in DT to add custom metadata. From my knowledge, I can use Hazel to add tags prior to importing the files into DT, but using it to extract data from the file which may afterwards be used for custom metadata is an unknown setup for me.
Are you aware that something like this would be working?

I’m sorry, I’m not sure I can help any further. In my current set up, I don’t import files into DT, I keep them in a huge iCloud folder with tons of nested folders and I index my whole file system into DT. So I can have Hazel do its magic on the files and then see them indexed in DT. I guess you import your files into DT so I don’t know how to mimic the Hazel macros in DT.

Since DT is scriptable, you can do the same things in DT as in Hazel. And sometimes a bit less convoluted :wink: I posted a sample script a while ago here

that shows how one can do it.