Regular Expressions and Smart Rules v3.5

If it’s a Date, you should store a Date, not a String.

Good point. I’m sorry, but I’m still not getting the date into the Date field. I believe this is how you meant for me to do this? I get a date and try to place it into the duedate field, but nothing changes.

When I run the rule from inside DevonThink instead of Script Editor the log in DT shows; which seems a bit vague.

2023-10-23_12-51-38

I am not a JavaScripter, but this code works with your very hardcoded example…

function performsmartrule(records) {
const app = Application ("DEVONthink 3");
app. includeStandardAdditions = true;
	const RE = /Payment\/Defer Date\s*([0-9-]{5,})/; 
records.forEach(r => {
const txt = r.plainText();
const match = txt.match (RE);
if (match) {
console.log (match[1]);
var dateComponents = match[1].split("-");
const myDate = new Date(dateComponents[0], dateComponents[1] - 1, dateComponents[2]);
console.log(myDate);
const md = r.customMetaData() || {};
md["duedate"] = myDate;
r.customMetaData = md;
}
})
}

(() => {
  const app = Application.currentApplication();
  if (app.name() !== "DEVONthink 3") {
    performsmartrule(Application("DEVONthink 3").selectedRecords());
  }
})()

I think the only thing you changed is the RegEx? Anyway I tried that. Yes the log looks like it get’s the right date but the custom metadata Date field is not updating upon running the script.

Have you tried with more than one file?

Shouldn’t that be mdduedate instead of duedate? I also used mdnumber in my sample code. AFAIK, the prefix md is required in these cases.

And please do not post code as screenshots. That’s completely useless – who want’s to type in all that just to find an error?

Apologies, new to the this still. Yes the md prefix fixed it. Is there a place in the documentation that explains the rules and shows examples for what I am trying to do with Custom Metadata so I can stop bothering you?

@bluefrog is better placed to answer that. There’s a brief mention in the “Search prefixes” appendix in the manual (Custom metadata). I have a hunch that I’ve read about the md prefix for custom metadata, but I can’t find that now.

In any case: Your custom metadata have a type that you specify when you define them. The values you want to store in the metadata field must match this type. And there might be types that you can’t use with JavaScript, like rich text.

Thanks!

Actually DEVONthink tries to convert the values if necessary & possible (e.g. strings to numbers and vice versa)

Would it be possible to raise an error if that doesn’t work or if there’s no match for the custom metadata field, eg if the md prefix is missing?

DEVONthink automatically handles this too (just tried successfully)