Christian, Jim, and others, first of all, I’m amazed by the level of passion and dedication you demonstrate here. For that, I thank you from the bottom of my FCK heart
Being a software developer myself, I’m never satisfied with workarounds or the “let it go and do it some other way” approach. If a feature (like JXA automation) is made available, it must work (and have bugs, since it’s software). So, I’ll keep digging until I see that there is no chance for it, and eventually, and sadly, accept the need to “let it go and do it some other way.”
So let me continue trying…
You said this:
Yes, osascript and Script Editor should be basically the same. But so should DT’s JXA scripting. Or, in other words, DT is using the same infrastructure as osascript and Script Editor.
… which opened my eyes to something.
Although it is true that DT uses the same infrastructure, there’s an important difference that we didn’t pay attention to. In the case of Smart Rules, DT is the one preparing the list of records and sending it to performsmartrule(records)
. So, I intuitively thought that maybe the list built by DT is different than the one my script builds. And voila, the issue has been located.
The TC is simple. Here’s my original code, which doesn’t work:
function performsmartrule(records) {
let app = Application('DEVONthink 3');
var group = app.createLocation('/Test', {in: app.inbox});
records.forEach(function(record) {
// This line throws an error
app.move({record: record, to: group});
});
}
I then tried something else, to not use the records
sent by DT and build my own list instead, and everything works:
function performsmartrule() {
let app = Application('DEVONthink 3');
// Get all records in the inbox,
// instead of relying on the records sent by DT
let records = app.inbox.root.children().filter(function(record) {
return record.kind() != 'Group';
});
var group = app.createLocation('/Test', {in: app.inbox});
records.forEach(function(record) {
// This line now works
app.move({record: record, to: group});
});
}
So, “theoretically” we have similar records
lists, but there is something in the internals that messes things up. I tried to figure out what, with no success, certainly because of my limited knowledge of JXA and DT.
It is important to note that this issue happens when calling this code from a Smart Rule in DT, so DT will build the records list itself.
I hope this will help those who know more to understand and eventually fix this issue.
The nice outcome of this research is that a “reasonable” workaround could be found. It basically involves rebuilding the records
list within the script:
function performsmartrule(records) {
let app = Application('DEVONthink 3');
// Rebuild the record list since the one passed by DT can cause issues
records = records.map(function(record) {
return app.getRecordWithUuid(record.uuid());
});
var group = app.createLocation('/Test', {in: app.inbox});
records.forEach(function(record) {
// This line now works like a charm
app.move({record: record, to: group});
});
}
I assume the above will become part of my default template when developing Smart Rule scripts in the future… at least for the time being