Sorry, I can't be of much help here. I've only used DT Pro lightly, and have never tried to script it -- either with AppleScript or JXA.
Seems like @ComplexPoint has laid out the underlying issue very clearly.
It's really a shame that
Having said all that I would add two points:
- As @ComplexPoint has often mentioned, there can be real problems with the Script Editor
- So, avoid using it if you can -- I now prefer VSCode
- Always use a top-level function with the JXA and JavaScripts, regardless of the editor.
- You may be interested in a JXA template I have just posted:
MACRO: [JXA] Template -- KM