Creation date based on Ulysses sheet contents -- AppleScript

Hi guys!

Hope you are all keeping safe and well! :grinning_face_with_smiling_eyes:

I’m in need of an AppleScript to help speed up the process of updating the creation date of files I have indexed from the Ulysses library. Currently this is a manual process but I have over 1000 notes so looking for some help.

What I’m hoping to achieve:

  1. If date is found within the selected indexed Ulysses sheets contents in the following format YYYY-MM-DD
  2. Then update the Creation date of the Ulysses sheet to YYYY-MM-DD (excluding the time as I want to retain this)

The script doesn’t have to be specific to Ulysses sheets as I’m (nearly) certain any script that has the ability to read the contents of the text file will work.

I’d be grateful for any help getting this up and running.

Thank you for your help in advance! :slight_smile:

p.s: I am based in the UK

Why? Not that it’s not feasible, but I do not really understand why you want to preserve the time of the file creation but not the date. Anyway, here’s a JavaScript that might do what you want. I didn’t test it though. It loops over all selected records and adjusts their date/time according to your requirements, if it finds YYYY-MM-DD in the text of the record.

Disclaimer As I said, it is not tested. Use it at your own risk, test it with copies of your data. And I do not even know if it does what you want it to do with indexed files. In other words: An indexed file has a creation date set by the operating system. I have no idea if DT will change that if you set the creation date of its record.

(() => {
const app = Application("DEVONthink 3");
const records = app.selectedRecords();
const  dateRE = /(\d{4}-\d\d-\d\d)/; /* Regular expression for the date */
records.forEach(r => {
  const match = r.plainText().match(dateRE);
  if (match) {
   /* There was a date found (possibly) */
    const oldDate = r.creationDate();
    let newDate = new Date(match[1]);

    /* Set the new date's time to the old ones */
    newDate.setHours(oldDate.getHours());
    newDate.setMinutes(oldDate.getMinutes());
    newDate.setSeconds(oldDate.getSeconds());
    /* set DT creationDate to the new date + old time */
    r.creationDate = newDate;
  }
})
})()

Good to know, but is that relevant to the task here? Just in case, I’m living in Germany, but I think you may use the script even though the UK left the EU. Or are you worried about time zones? Imperial vs. metric system?

Are you looking for any date or is there any context for the date, e.g., Date: 2021-12-04 ?

Computers are dumb and generally just obedient. You need to be specific about things you tell it to do.

Hi @chrillek

Thank you for your help I will test this (carefully) later when I’m back behind my laptop.

The reason I don’t want to change the time in this instance is because I have split a single Ulysses sheet (a daily note) into multiple sheets, all created on the same day. By leaving the time unchanged the split sheets retain a semblance of chronology as to what was created first, what next and what last.

Regarding U.K… I had an issue with a script @BLUEFROG kindly helped me out with a while back and I needed to tweak it to suit my locale.

Once again, thank you :relaxed::pray:

Hi @BLUEFROG

The date is specifically formatted yyyy-mm-dd and is located at the very top of each note.

And the time is included in that first line?
Post an example, if you would.

Here’s a screenshot :slight_smile: thank you @BLUEFROG

Time is not required for my use, as per explanation above. I need to retain some semblance of chronological order for the sheets that have been split via Ulysses, so ideally I would like to just change the year, month and day.

The first 10 characters of the content?
That makes it easy to locate

Hi @DTLow, that is correct. I just need the AppleScript to read the first 10 characters of each note (which would be the date) and use this information to set the creation date of the note (excluding time).

What do you get when you run this in Script Editor…

set r to "2020-12-01"
date r

I’m sorry, I don’t know if I entirely understand… but here is the screenshot.

And what does it report when you run it - press the little play button in the toolbar.

result

date “Tuesday, 12 June 2007 at 00:00:00”

Funny. What’s wrong with the script I posted?

Do you get the same result from that two-liner, @chrillek ?

Yes, with a germanized Tuesday
Edit I get the exact same result with other dates:

set r to "2020-12-01"
log date r
set r to "2020/12/01"
log date r
set r to "2020.12.01"
log date r
set r to "2020-01-12"
log date r
set r to "2020/01/12"
log date r
set r to "2020.01.12"
log date r

I.e. every single one prints (*date Dienstag, 12. Juni 2007 um 00:00:00*). Is there something special about this date - maybe the first time Apple ever paid a dividend? I don’t even see how any piece of software could deduce June 12th 2007 from a string like “2020-12-01” or “2020-01-12”. … Oh, yes, now I do: 2007 is the result of 2020-01-12. Well… Always nice to have software that does the “thinking” for you… (but then, shouldn’t “2020/1/12” give something like 168,333?)

Curiouser and couriouser. I search-engined a bit and found for example this text - Applescript : String to Date - Ask Different on using NS… to format a date (which only works by recurring to the locale settings, of course).
The problem with dates is simple: There are several regional customs to write them (dd.mm.yy(yy) in some parts of Europe, mm.dd.yy(yy) in the US, yyyy-mm-dd in ISO-land) and it is impossible to figure out what is the dd and what the mm part from simply looking at the date. So, in the case of the OP, we know that they’re using ISO and can work with that (well, AppleScript can’t, apparenty, because it prefers to perform a subtraction in the middle of a string and invent a day and month). But it seems(!) that AppleScript only understands a “dd/mm/yyyy” date string. At least, the code works with that format.

But if one simply extracts the date string as it is (ISO date!), that should work just fine in JavaScript.

1 Like

aaaahhhh, you’re right! This was puzzling me to no end!

It still puzzles me: Why would a self-respecting programming language perform a subtraction inside a string? Or kind of, because r as number gives an error. This is so crappy…

Very, very strange indeed.

Hi Bluefrog, just checking in to gauge if my request is do-able via AppleScript or a pipe-dream :crazy_face: before I attempt to use the Javascript @chrillek kindly provided.