Extracting numeric date from file content to use in renaming file

I have a PDF file with a line in it like this:

Statement Period: 11/1/2023 - 11/30/2023

I have a smart rule that uses regular expressions to match that and add “2023 11 30” to the filename.

But when the month is only a single digit, I’d like to have a leading zero if possible so that filenames will sort by date order properly.

For example, a file containing:
Statement Period: 4/1/2023 - 4/30/2023

would add “2023 04 30” to the filename.

I have a hunch that’s not possible using ICU regular expression notation. (It might be possible with PCRE but wouldn’t be simple.)

Is there a way to do this with Scan Text->Date?

(I don’t understand AppleScript nearly well enough to write an AppleScript to do that.)


(:grin: How about Python scripting in DEVONthink? Not expecting that, but it would be amazingly powerful. And probably very difficult to implement well.)

No, this can be done without scripting of some kind . There aren’t conditionals in a smart group and a RegEx isn’t going to return a value that would produce zeropadding or not when using the capture groups.

This is unlikely. But you understand Python and not AppleScript?!? That always boggles my mind since AS is such a simple language to learn.

There’s a lot that’s unfamiliar to me in the AppleScript paradigm, given that I started programming in 1976. I’ve worked a little with some really unusual languages like APL and Smalltalk and even a little with more obscure approaches like ProGraph, but AppleScript just seems really cumbersome and wordy, along with dealing with so many kinds of objects and all their attributes.

Maybe I need to look at more DEVONthink-oriented AppleScript?

1 Like

Note the filename and the dates in the text, including the lack of zeropadding and the differing delimiters (slash vs hyphen).

A simple smart rule with Scan Text - Date and changing the name using the Newest Document Date, i.e., the closest one to today…

Actually, in this case you can just the Document Date placeholders. Six of one; half a dozen of the other :slight_smile:

And the resulting filename change.

1 Like

Instead of AppleScript, you could use JavaScript. That’s close to a modern programming language like Python.

Some info to be found here: Scripting with JXA | JavaScript for Automation (JXA)

As to Python: yes, you can use it to script DT, too (I guess). Just not from within DT itself, since it’s not part of Apple’s scripting infrastructure.


Might be of interest. I use Python a lot in my analysis work, but I’ve not looked further into this as linked below. Looks interesting and on my “todo” list someday:

1 Like