JavaScript behaves erratically

What I want to achieve: rename a file saved in my Downloads folder (that works) and save it in a particular group in DEVONthink Pro 3 (that doesn't work). Basically I want to add account statements to the correct group in DT3.
I wrote a JS script (see below) that
- works ok if I run it in Script Editor and feed it the required parameters (see commented out section at the bottom of the script)
- throws an OSA script error if I run it as an embedded script or external script from Hazel
The output of the messages I added to the script is identical in any three cases. Since the dialog directly before it works, the problem seems to arise in the import command. Does anyone have an idea on that?
I wrote a JS script (see below) that
- works ok if I run it in Script Editor and feed it the required parameters (see commented out section at the bottom of the script)
- throws an OSA script error if I run it as an embedded script or external script from Hazel
- Code: Select all
2020-05-13 12:39:42.510 hazelworker[27467] OSAScript error: {
OSAScriptErrorBriefMessageKey = "Error: Error: Can't get object.";
OSAScriptErrorMessageKey = "Error: Error: Can't get object.";
OSAScriptErrorNumberKey = "-1728";
OSAScriptErrorRangeKey = "NSRange: {0, 0}";
The output of the messages I added to the script is identical in any three cases. Since the dialog directly before it works, the problem seems to arise in the import command. Does anyone have an idea on that?
- Code: Select all
function hazelProcessFile(theFile, inputAttributes) {
var accounts = {
"05140xxxx": ["ck Privat", "ING"], /* accounts obfuscated here */
"5114xxxxx": ["ck Privat", "Volksbank"],
};
var app = Application('DEVONthink 3');
app.includeStandardAdditions = true;
app.displayAlert(theFile + "/" + inputAttributes[0]);
var accountNo = inputAttributes[0];
var dbName = accounts[accountNo][0];
var groupName = accounts[accountNo][1];
var db = app.databases.whose({name: dbName});
var g = app.search("name:" + groupName + " kind:group kind:!tag", {in: db})[0];
app.displayAlert(g.name() + "/" + g.database.name());
app.import(theFile, {to: g});
}
/* This part uncommented allows to run the script in Script Editor */
(() => {
let file = "/Users/ck/Downloads/KA 04-2020 5114218008 2020-04-30.pdf";
let attr = ["5114218008"];
hazelProcessFile(file,attr);
})();
*/