Automating DT with JavaScript: Programming Environment

As with AppleScript, your first stop in writing a JavaScript script for DEVONthink is the script editor that is pre-installed on every Mac. It is the only source for information on DT’s objects, methods and properties.

So fire it up and load the function library for DT. Make sure to set its language selector to “JavaScript” and keep this window open – you’ll need it often as you go along.

Which editor to use?

Now, what program you chose to write your scripts in is pretty much a matter of preference. I think that script editor is the worst possibility since it offers neither useful highlighting nor autocompletion nor reference information.

If you want something free and sturdy, Visual Code Studio might do the trick. I prefer CodeRunner because it’s not so huge and easier to configure. The money spend on it is well spent (in my opinion).

I’ll stick with CodeRunner here. To use it for JavaScript, you duplicate the language definition for npm and change the “run command” to osa -l JavaScript $filename. Give this new definition a name like “JavaScript (JXA)” and start coding.

CodeRunner will alert you to syntax errors before you run the script. While you type, it will propose sensible completions and show the definitions of standard methods. That’s of course helpful, but it does not extend to DT’s (or any other program’s methods). This is one of the (many) shortcomings of JXA: the objects are mostly opaque and do not provide for introspection.

What about debugging in JXA?

What about it, indeed? Short answer: forget about it.

Slightly longer answer: use console.log().

Even longer answer: you could add a line containing only debug; to your code. This will open Safari’s web inspector at the line right after the debug;. Well, sometimes. Oftentimes not. Even if it worked s as advertised, all you can do is step through the code. Safari knows zilch about JXA objects, so no inspection of properties etc. I think that’s nothing more than a glorified console.log(). If you like a GUI to walk you through your code, try it out.