Thank you for all the observations. It is a big guess work to understand the JXA API of DT. But I’m getting used to the dictionary documentation now (although I still don’t know when to use dereferencing).
I applied your suggestions and came out with the following:
function performsmartrule(records) {
let app = Application('DEVONthink 3');
let group = app.createLocation('/Test', {in: app.inbox});
app.logMessage('Number of records: ' + records.length);
records.forEach(function(record) {
app.logMessage('Record: ' + record.name());
app.move({record: record, to: group});
app.logMessage('Moved: ' + record.name());
});
}
// Set to false to run as a smart rule
if (true) {
let app = Application('DEVONthink 3');
// Get all records in the inbox
let records = app.inbox.root.children().filter(function(record) {
return record.kind() != 'Group';
});
performsmartrule(records);
}
I noticed there there is a bit of confusion around osascript
so I’m rephrasing things here:
- The above script runs well when doing “play” in Script Editor (this is basically the same as running it with
osascript
). - Now set the if check to “false” and save the script in the “Smart Rules” script folder of DT.
- Create a smart rules for the Inbox on files of kind “Any Document” that runs on demand and executes the saved script.
- Be sure to have some files in the inbox.
- Apply the rule.
For me, the “Test” group is created. The log shows 3 entries: the proper number of records, the “Record:” message for the first document and the error.
I’n other words, this issue happens only when the script runs inside DT itself.
Thanks you a lot for the amazing help!