Creation date based on Ulysses sheet contents -- AppleScript

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.

It likely is possible but we are discussing some date stuff internally. If @chrillek’s works, I’d suggest using it.

Thank you Bluefrog. Please do update me with your findings once you have them. I’ll try test @chrillek 's suggestion later this week and report back :+1:

No problem

Hi Chrillek :slight_smile:

I have finally found some time to sit down and try out your Javascript, but I have hit a hurdle right out the gate. Please see attached image:

I’d be grateful for your help :pray:

There were some parenthesis missing, unfortunately (sorry, @Blanc). I’ve amended the script in the original post.

1 Like

It’s a global supply chain issue :man_shrugging:

2 Likes

Thank you, that worked a treat! :grinning_face_with_smiling_eyes: :pray: :+1: Is there a way I can automate it to run on a selection rather than individually?

@chrillek My bad! I’ve just figured it out! Instead, could I ask for some help now turning this into a smart rule?

This has been described in other posts already. Search for “performsmartrule”, for example. And check the documentation’s section on Automation – it’s all explained there.

1 Like

Thanks Chrillek. Okay, so I found your previous post (JavaScript: Using a script stand-alone and in a smart rule](JavaScript: Using a script stand-alone and in a smart rule) and I attempted to replicate, but I’m getting an error.

I’d be grateful if you could help steer me toward a solution.

screenshot 2021.12.11.11.38.36

Thank you for your time and effort, I really do appreciate your help as not very versed in scripting and automation.

I’d wager you’re missing two } -

So I think the line with the single } needs to be followed by another two lines each with a }

Also I suspect you don’t want the first (() => { nor the second and third lines in your script (const app and records.) as you are using those commands twice within the script.

Then again I haven’t got a clue :see_no_evil: But I’m pretty sure each open parenthesis needs to be closed somewhere along the way :wink:

Hi Blanc, thank you for jumping in :slight_smile:

I’ve attempted to follow your suggestions (I probably haven’t interpreted correctly) and this is what I get with an error on line 23.

function performsmartrule(records) {

(() => {
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;
  }

performsmartrule(Application("DEVONthink 3").selectedRecords())
}

I’m hopelessly lost :woman_shrugging:

Sorry, I haven’t explained very well - I’ve now moved from iPad to iMac, so here goes. What I meant was:

function performsmartrule(records) {
const app = Application("DEVONthink 3");
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;
  } /* this one closes the if condition */
 }) /* this one closes the repeat cycle */
} /* this one closes the function */

(() => {
performsmartrule(Application("DEVONthink 3").selectedRecords())
})()

(give me two minutes after posting, I’m just going to read through it again)

EDIT: missing a ) on the } closing the repeat cycle
EDIT 2: removed dual selection of records

1 Like

I see, you’re getting the hang of it. Be careful not to develop an addiction to parenthesis!

2 Likes

I’m still hitting an error?

screenshot 2021.12.11.12.19.09

correct; delete that line, please - the records are selected by the smart rule (line one).

Please excuse that we are doing this by trial and error; I have never written let alone debugged a JS to date (which is why @chrillek & I have this running gag on parenthesis on this forum - I’m reasonable at AppleScript and find JS pretty inscrutable - by I’ve endeavoured to try and get a grasp; chrillek, in contrast, is the resident JS genius).

1 Like

Bingo! Thank you Blanc :slight_smile: :raised_hands: I’ll try running the smart rule now

1 Like

Do it on copies of your data, please!

1 Like

Will do :+1: thanks again for your help :slight_smile: