How to add a JXA file to the Smart Rules folder

This is my first smart rule that uses JXA.

With lots of help from @chrillek I have a complex JXA script that runs perfectly when embedded within the smart rule.

I would prefer to have the script be loaded from an external file but ran into issues when adding my script to the ~/Library/Application Scripts/com.devon-technologies.think/Smart Rules folder.

I first tried to add a sub directory, but that wasn’t recognized in the drop down in the Smart Rule editor.

Fair enough, so I added my script at the top level with the other scripts already there, using a prefix to visually separate my script from the others.

I tried adding the .js file, containing the working code used in the in-line version of the smart rule, but that file didn’t show up in the menu.

Next, I manually changed the suffix from .js to .scpt, then the file showed in the menu, but quickly produced an error on performSmartRule (A "/" can’t go here.) which I believe indicates that DEVONthink is running this as an AppleScript.

Finally, I took the script and compiled it into a scpt file in Script Editor with the language set to JavaScript, and … that works as an external file from a smart rule!

When I look at this file in BBEdit, it has some non-ascii characters at the end of the file and that seems to contain the magic sauce.

Can someone please tell me if this is the only workflow to get a JXA script with a .js file type to load externally from the Smart Rules folder.

It should be a .scpt file.

As a follow on …

Although the code works, in the sense that it correctly auto files my statements, as intended in both cases. There is a difference whether the code is inline in the smart rule, or loaded from an external file.

The inline code produces all my logged debug messages in the DEVONthink log window, while the externally loaded code only shows a subset of those messages.

I removed a number of the problematic individual logs that were missing in the external case, and I replaced those log calls with a single log of the object via:

_log(JSON.stringify(obj));

Where:

function _log(msg) { // DEVONthink log window
  Application("DEVONthink").logMessage(msg);
}

Now this is the one log message that is problematic. It is present when running the inline code, but not when running the external compiled script as shown above.

This seems strange…

===inline code===
2/16/26, 7:24:21 PM: +++
checkUidMap: <c_marina>
2/16/26, 7:24:21 PM: checkUidMap: processing uidMap key: Waterfront Dept.
2/16/26, 7:24:21 PM: {"tags":["autoFiled"],"keyPhrase":"To:","monthOffset":0,"db":"JXA Test","group":"c_marina","name":"Marina","dateLocale":"US","dateType":"month"}
2/16/26, 7:24:21 PM: checkUidMap: all entries have handler, keyPhrase, monthOffset, group, and database set
2/16/26, 7:24:21 PM: checkUidMap: database id: 2
===external code===
2/16/26, 7:25:03 PM: +++
checkUidMap: <c_marina>
2/16/26, 7:25:03 PM: checkUidMap: processing uidMap key: Waterfront Dept.
2/16/26, 7:25:03 PM: checkUidMap: all entries have handler, keyPhrase, monthOffset, group, and database set
2/16/26, 7:25:03 PM: checkUidMap: database id: 2

I have used AppleScript to highlight the JSON string that only shows with the inline code. This is copied directly from the DEVONthink log window.

Typo in one instance scpt instead of .scpt.

In all cases I am talking about setting the file type extension to .scpt instead of .js.

If I do this manually, the script doesn’t show up in the drop down in the Smart Rule editor, as described.

.scpt files certainly do show up in the popup, regardless of the language used.

I agree.

Does that also mean that .js files and subfolders do not show up, as I have experienced, by design?

The mystery to me is that since JavaScript isn’t really compiled, it’s interpreted, then I would expect that I could just rename a .js file to .scpt to meet the design requirements and have it run.

But it seems that I actually need to “compile” it in the Script Editor.

Is that the case?

If so, then I have the problem that not all my log messages show up in the log window when I use the “compiled” version.

Thanks, Paul

The extension .scpt indicates a compiled script. And the compiled code does contain a language indicator.
So, there’s no way to have a text file with whatever extension as an external script.
That’s one reason why I never use them in smart rules. Also, external scripts might be cached, which makes developing/modifying them more trouble than with internal scripts.
And, btw, AppleScript is not compiled either. Or rather: you can pass text in both languages to osascript for execution. But then you need to pass the language as a parameter, too.

Btw: in many environments. JS is compiled internally for faster execution.

Thanks for the clarification. Based on this, I will use in-line JXA scripts moving forward.

The next release will also support scripts having the extension .js, e.g. in the Scripts menu but also by smart rules and batch processing.

2 Likes

That will be very convenient - thanks!