I am currently in the process of moving most my documents from my finder storage to DEVONthink to have everything in one place and use this opportunity to rename the files. Unfortunately I am having a hard time with batch renaming in DEVONhtink. I used to store dates in the format YYMMDD and would like to change these to YYYY-MM-DD but cannot figure out how to do this easily.
Could anyone please help me or does have a better idea? One other idea I had was to rename them with a renamer program prior to importing to DEVONthink but that changes the document date (last changed) which I need for categorizing.
And considering how mighty RegEx seem to be, does anyone have a good source for learning them (total beginner)? Thanks!
The title of your post seems to indicate the exact opposite of what your text says Let’s stick with the text, then the search expression would be (\d\d)(\d\d)(\d\d) (YYMMDD)
and the replace expression 20\1-\2-\3 (20YY-MM-DD) if you’re using the rename with regular expression script in DT. And things get hairier if your years include the previous century…
There’s a batch rename option, too. But I’m not at my Mac now, so I can’t check that.
Ok, I tried it with a Smart Rule and the batch renaming and I successfuly changed the date format. Unfortunately I did also remove the rest of the file name. How do I tell DEVONthink to keep the rest of the name? Thanks!
My criteria:
scan name - regular expression - (\d\d)(\d\d)(\d\d)
change name in 20\1-\2-\3
I seriously suggest reading and learning about regular expressions – otherwise you’ll be stuck in a “recipe” trap and get lost there when the cook is out.
Assuming your file names are somethingYYMMDDsomething else, (.*)(\d\d)(\d\d)(\d\d)(.*)
will match them, and \120\2-\3-\4\5
Will change only the YYMMDD part. Hopefully.
Nice! I will keep that in mind when I start learning.
By the way: In case your iPad or iPhone App is too small for macOS, this tool can change the text size of the apps. Haven’t tested it yet but heard a lot of good about it.
Applescript would have been my solution
Regex isn’t supported, but I find the coding clearer
Something like
tell application id "DNtp"
set theSelection to get selection
repeat with theNote in theSelection
set theName to name of theNote
set newName to "20" & texts 1 thru 2 of theName & "-" & texts 3 thru 4 of theName & "-" & texts 5 thru -1 of theName
set name of theNote to newName
end repeat
end tell
Cleaner? In the eye of the beholder, probably. But that solution suffers from the same problem as my first attempt. Worse: it works only with names beginning with YYMMDD. And it did not even check if that’s what the name begins with.
While a regular expression simply does nothing of it doesn’t match the given pattern. More robust.
Verification would be a useful addition
I think the applescript code would be If length of theName is >= 6
If texts 1 thru 6 of theName is integer
(Not tested)
That does not compile on my machine. You want ‘text’ - singular.
It also is not safe:
text 1 thru 6 of "23051 " as integer --> no error; trailing space is ignored
You need to check that each & all of the 1st 6 characters are integers:
set t to "230513 should be a date"
set isDate to false -- safeguard against other errors
repeat with i from 1 to 6
try
text i of t as integer
on error
set isDate to false
exit repeat
end try
set isDate to true
end repeat
if isDate then
-- rename this item
end if
For what it’s worth: Tasks like writing that AppleScript are routinely solved by ChatGPT. I’ve not checked the following output, but after a couple of iterations it usually works pretty well. So no guarentees, just something to keep in mind.
Edit: After consideration, it might be a bad idea to rely on chatGPT for a more rare language like AppleScript. In my opinion, it can still give you ideas. Always check code that you copy from the Internet!! be it human or machine generated (and who knows which is which anyway).
And routinely, ChatGPT creates wrong code. Especially for AppleScript. We’ve been there. So, what is it worth?
Why are you posting possibly bad or wrong code? And what, frankly, is the point of posting worse code than has been posted before to solve the same problem (not only in this thread, but many times in this forum)? And why use a screenshot so that nobody can copy/paste the stuff, instead of actual code?
And you can, of course, use regex101.com or other sources to learn something. Be it AppleScript or JavaScript or Regular Expressions. Instead of having a glorified ape demonstrate that it can do some kind of text analysis (Hello Eliza!) and throw back code at you (and all of us) that is neither new nor good nor robust.
And this thing is even just blabbing: “Adding” (rather “prepending”) the string “20” to another string is by no means a “logical operation” (as there are no logical operators involved), it is a simple string concatenation. Which, again, proves that it doesn’t know anything. It just does text processing.