JavaScript working in Script Editor but not Hazel

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

Moderator: Mr_Noodle

Hi,

This is my first post and my first time developing a script for Hazel.

I have a simple (short) JavaScript script that attempts to import a file into a specific group in DEVONthink 4. I am using it as an embedded script, so my understanding is that I do not need a handler and the file to process will be available to my script as theFile.

I have tested this script in Script Editor by simply adding a constant declaration to initialize theFile and it works as expected. Unfortunately it fails in Hazel.

Here is the script:
Code: Select all
// Hazel script to move `theFile` to a fixed folder in DEVONthink.
const theID = "C98D0C6F-DADB-4146-9D40-860287F63915"; //JXA Test > Inbox > _Statements

// comment out the next line when running as an embedded JavaScript in Hazel
const theFile = "/Users/paul/Downloads/_Statements/Unknown.pdf";

//DEVONthink
const dt = Application("DEVONthink");
dt.includeStandardAdditions = true;

let params = {};
let destGroup = dt.getRecordWithUuid(theID);
params.to = destGroup;
const rec = dt.importPath(theFile, params);


This works as expected from Script Editor and the Replies window shows:
app = Application("DEVONthink")
app.getRecordWithUuid("C98D0C6F-DADB-4146-9D40-860287F63915")
--> app.databases.byId(2).parents.byId(9464)
app.importPath("/Users/paul/Downloads/_Statements/Unknown.pdf", {to:app.databases.byId(2).parents.byId(9464)})
--> app.databases.byId(2).contents.byId(9646)
Result:
Application("DEVONthink").databases.byId(2).parents.byId(9464)


However, when running the script in Hazel, the logs show:
2026-02-18 17:19:25.133 hazelworker[5482] DEBUG: Tapping error retry sequence
2026-02-18 17:19:25.134 hazelworker[5482] Unknown.pdf: Rule move to DEVONthink matched.
2026-02-18 17:19:25.134 hazelworker[5482] DEBUG: Manual run. Forcing actions to be executed.
2026-02-18 17:19:25.156 hazelworker[5482] [Error] JavaScript failed: Error executing JavaScript on file /Users/paul/Downloads/_Statements/Unknown.pdf.
2026-02-18 17:19:25.156 hazelworker[5482] OSAScript error: {
NSLocalizedDescription = "Error: Error: Can't get object.";
NSLocalizedFailureReason = "Error: Error: Can't get object.";
OSAScriptErrorBriefMessageKey = "Error: Error: Can't get object.";
OSAScriptErrorMessageKey = "Error: Error: Can't get object.";
OSAScriptErrorNumbe
rKey = "-1728";
OSAScriptErrorRangeKey = "NSRange: {0, 0}";
}


Most of the code runs in Hazel, including the getRecordWithUuid() call which returns the right result, until it fails with the final importPath() call.I know this, because I had instrumentation to log debug information to the DEVONthink log window, when running in Hazel, that I have since removed for simplicity to demonstrate the problem.

Consequently, I believe that it is the importPath() call that is generating the error messages seen in the Hazel log

As an aside, is there a way for me to log debug information to Hazel from my script - console.log() doesn't seem to work unless there is a switch that I need to set in the log window that I didn't notice.

Thanks in advance for any insight.
farf
 
Posts: 2
Joined: Wed Mar 02, 2016 4:10 pm

Are you on Tahoe? If so, I've found JavaScript (but not AppleScript) to be broken.

Try a simple script that just returns and see if that works at all.
Mr_Noodle
Site Admin
 
Posts: 12158
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

No, I am on Sequoia 15.7.3.

However, I believe that I have solved the problem with help from a user on the DEVONthink forum.
In that post https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635 a key point is made:
In the import() command, theFile.toString() is essential:
- Hazel passes an alias to a file to the script,
- import() expects a string.
Note: the import() JXA action in DEVONthink is now importPath() as of DEVONthink 4.


So, I made the change to my script:
Code: Select all
(() => {
   const theID = "C98D0C6F-DADB-4146-9D40-860287F63915"; //JXA Test > Inbox > _Statements

   //DEVONthink
   const dt = Application("DEVONthink");
   dt.includeStandardAdditions = true;
   
   let params = {};
   let destGroup = dt.getRecordWithUuid(theID);
   params.to = destGroup;
   const rec = dt.importPath(theFile.toString(), params);
})()
and that works as expected!

Do you mind expanding on the alias comment and perhaps it should be added to the documentation, if not already?
I assumed that theFile was a valid string when I was able to output it via a log message to the DEVONthink log window, but that log function must have made an internal type conversion from alias -> string?
farf
 
Posts: 2
Joined: Wed Mar 02, 2016 4:10 pm

From the manual:
Code: Select all
The variable  theFile  carries an alias to the file or folder currently being processed, which Hazel passes to the script.


I could expand on it more but it is clearly stated there.
Mr_Noodle
Site Admin
 
Posts: 12158
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support