"Can't find variable: theFile" error w/external JavaScript?

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

Moderator: Mr_Noodle

I'm not particularly adept at JavaScript, but I need to use it in order to pass multiple arguments constructed from Hazel content matches to an external Python script. I'm having problems with the external JavaScript failing because of a "ReferenceError: Can't find variable: theFile" error.

I have also experienced the same error with the inputAttributes variable/array, but I can't seem to figure out why the JavaScript is not seeing either of those variables, even though I did setup the hazelProcessFile(theFile, inputAttributes) handler in the script, as mentioned in the "AppleScript/JavaScript in Rule Actions" section of the Hazel documentation?

Here's a snippet of a typical error from the Hazel logs:

Code: Select all
2024-05-30 15:08:16.565 hazelworker[28127] Processing folder tmp
2024-05-30 15:08:18.853 hazelworker[28127] 2024-05-27 - Setapp-1.pdf: Rule Receipt: SetApp (Mimestream Export as PDF) matched.
2024-05-30 15:08:18.894 hazelworker[28127] [Error] JavaScript failed: Error executing JavaScript on file /Users/jappleseed/tmp/2024-05-27 - Setapp-1.pdf.
2024-05-30 15:08:18.895 hazelworker[28127] OSAScript error: {
    NSLocalizedDescription = "Error: ReferenceError: Can't find variable: theFile";
    NSLocalizedFailureReason = "Error: ReferenceError: Can't find variable: theFile";
    OSAScriptErrorBriefMessageKey = "Error: ReferenceError: Can't find variable: theFile";
    OSAScriptErrorMessageKey = "Error: ReferenceError: Can't find variable: theFile";
    OSAScriptErrorNumberKey = "-2700";
    OSAScriptErrorRangeKey = "NSRange: {0, 0}";


This is the Hazel rule I'm using:

Image

And this is the external JavaScript code in the "hazel_pdf_parser" script:

Code: Select all
var app = Application.currentApplication()
app.includeStandardAdditions = true

function hazelProcessFile(theFile, inputAttributes) {

   let category = ""
   
   switch(inputAttributes[4]) {
    case "Adobe":
      category = "Software Subscription"
      break;
   case "SetApp":
     category = "Software Subscription"
      break;
    case "Amazon.com":
      category = "Equipment"
      break;
    default:
      category = "N/A"
   }
   
   return {
      invoiceDate: inputAttributes[0],
      item: inputAttributes[1],
      pretaxTotal: inputAttributes[2],
      grandTotal: inputAttributes[3],
      category: category,
      vendor: inputAttributes[4]
   }
   
}


let hazelValues = hazelProcessFile(theFile, inputAttributes)

let pythonArgs = Object.values(hazelValues).map(val => `"${val}"`).join(" ")

app.doShellScript('source /Volumes/VENVS/gsheet_api_test/bin/activate; /Volumes/VENVS/gsheet_api_test/main.py' + " " + pythonArgs)
melorama
 
Posts: 7
Joined: Tue May 05, 2009 10:25 pm

Something seems odd in that your function doesn't even reference theFile. Have you tried removing all the code outside that function?
Mr_Noodle
Site Admin
 
Posts: 11945
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:Something seems odd in that your function doesn't even reference theFile. Have you tried removing all the code outside that function?


The function doesn’t reference theFile because I actually don’t need it for the purposes of the function.

However, just to test, I did try to add a “useless” variable assignment of theFile inside the function, and it still returns the same error.

Commenting out all of the other parts of the code outside of the hazelProcessFile() function does prevent the error from occurring, however, I’d like to return the values from the function as an object array, so I can build the list of Python arguments efficiently.
melorama
 
Posts: 7
Joined: Tue May 05, 2009 10:25 pm

My point is that the function doesn't refer to theFile so why is there an error?

I'm not following why you need the code outside the function. I think it's best to keep the script limited to what Hazel needs for this without any extra stuff.
Mr_Noodle
Site Admin
 
Posts: 11945
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support