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:
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)