Page 1 of 1

Anyway to know that a JXA/AS script was called by Hazel?

PostPosted: Mon Mar 23, 2026 1:20 pm
by farf
I am wondering if there is a reliable way in a JavaScript called by Hazel, to know that it was called by Hazel and not Script Editor or CodeRunner.

When developing JavaScript for a smart rule in DEVONthink, I am able to get the current app and switch accordingly in my script, but that doesn't seem to work for Hazel.

I can get the front most app but that doesn't seem robust.

The reason for the request is so that I can fill `theFile` when in dev mode, and skip that code when Hazel is the parent.

Re: Anyway to know that a JXA/AS script was called by Hazel?

PostPosted: Tue Mar 24, 2026 9:38 am
by Mr_Noodle
Is this an external script being shared by all programs? Hazel does require a specific handler so it would seem to me that you can do whatever Hazel-specific logic in that handler.

Re: Anyway to know that a JXA/AS script was called by Hazel?

PostPosted: Tue Mar 24, 2026 10:50 am
by farf
Thanks for the reply.

Unfortunately I didn't make myself clear.

What I am trying to do is have one piece of code that I can develop outside of Hazel, but then also call from Hazel once debugged.

My testing shows that in the case of JavaScript, the script is loaded and run before then calling the hazelProcessFile() entry point.

I have a self-executing function that is run each time that Hazel runs the script and I would like to place some logic in the "iife" that switches based on whether Hazel is running. This happens before entering hazelProcessFile().

Here is an outline of the code:

Code: Select all
/* anonymous self-executing function permits to run this in script editor etc     */
/* We need to determine if we are running in Hazel, so that `theFile` can be set  */
(() => {

   const thisApp = "Script Editor" // I want to dynamically detect Hazel or not Hazel here

   if (thisApp !== 'Hazel') { // don't run in Hazel to prevent hazelProcessFile() running twice
// logic goesj here to set `theFile` and any `inputAttributes`

  hazelProcessFile(fullPath, inputAttributes));
   } else { //Hazel
      // do nothing here
   }
})();