Need help with JXA script in smart rule

Trying to remove empty sections of daily notes via smart rule. Getting error on the last line.

Just started to experiment with JXA. Could someone help?

performsmartrule = records => {
    records.forEach(r => {
var str = r.plainText();

const reg = /(## Reading\n\n)|(## Health\n\n)|(## Reading Log\n\n)|(## Listening Log\n\n)|(## Log\n\n)|(## Activity\n\n)/g;
const newStr = str.replace(reg, "");
newText = newStr;
r.plainText = newText;

You may want to look at @chrillek’s site on JXA for DT - to be found here.

Amongst other things, you appear never to close the for.each(, so the penultimate line should be }). But if you check the site referenced above you will see that Christian uses this basic format for JXA in smart rules:

  function performsmartrule(records) {
    const app = Application("DEVONthink 3");
    records.forEach(r => {
    /* do something with the records matching the
       smart rule's condition */

Thank you! Now it works.

1 Like

In addition to the helpful comments by @Blanc:

You could put the script in script editor (setting the language selector to JavaScript) and have it compile there. Then you’ll see syntax errors like yours immediately.

In addition: the capturing groups in your RE are not needed, and you might want to move the ## out of the alternation.