JavaScript behaves erratically

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

JavaScript behaves erratically Wed May 13, 2020 6:46 am • by chrillek
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

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);
})();
*/
chrillek
 
Posts: 5
Joined: Wed May 13, 2020 4:44 am

Re: JavaScript behaves erratically Wed May 13, 2020 9:40 am • by Mr_Noodle
If it's an embedded script, you cannot have a function defined. Read the manual as it has more details but when embedded, the script is already in a function.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: JavaScript behaves erratically Wed May 13, 2020 9:50 am • by chrillek
Mr_Noodle wrote:If it's an embedded script, you cannot have a function defined. Read the manual as it has more details but when embedded, the script is already in a function.


I'm aware of that, and I read the manual.
For space's sake I didn't post both versions of the script. The embedded version does NOT contain the function stuff, only the external script did. Both throw the exact same error (and the embedded one wouldn't even run with the function definition, much less throw this error).
chrillek
 
Posts: 5
Joined: Wed May 13, 2020 4:44 am

Re: JavaScript behaves erratically Wed May 13, 2020 10:18 am • by chrillek
The problem was not in the function vs. not function part, but in the parameter theFile that Hazel passes to the script. One might think that it's a string, but in fact it is "an alias" (cf. https://www.noodlesoft.com/forums/viewtopic.php?f=4&t=6433).

So instead of
Code: Select all
app.import(theFile, {to: group })


one has to use

Code: Select all
app.import(theFile.toString(), {to: group })


to get to the file's name.
chrillek
 
Posts: 5
Joined: Wed May 13, 2020 4:44 am


Return to Support

cron