Import from Hazel to various groups - JavaScript

There’s a fairly old AppleScript importing from Hazel to a fixed DT group here. I suggest the following JavaScript version (see below). It offers more flexibility since it can be parametrized: It takes the file name as its first parameter and an array of values that can be filled in Hazel as its second. In my implementation, the first element of this array is just some code (a number or a tag or …) that permits the script to select the appropriate database and group.

This script can be used as “external” in a Hazel action. If you remove the line function and the last closing }, it works as an internal script in Hazel, too.

In the import command, theFile.toString() is essential: Hazel passes an alias to a file to the script, import expects a string.

function hazelProcessFile(theFile, inputAttributes) {

  var groupSelector = { 
               "1234": ["database1", "group1"],
               "tag1": ["database2",  "group2"],
/* add more lines as needed */
			   };
  var app = Application('DEVONthink 3');
  app.includeStandardAdditions = true;

  var groupSel = inputAttributes[0];
 
  var dbName = groupSelector[groupSel][0];
  var groupName = groupSelector[groupSel][1];
  var db = app.databases.whose({name: dbName});
  var group = app.search("name:" + groupName + " kind:group kind:!tag", {in: db})[0];
  app.import(theFile.toString(),  {to: group});
}

/* Remove comment to run the script from the script editor
(() => {
  let file = "/Users/ck/Downloads/file 1234.pdf";
  let attr = ["1234"];
  hazelProcessFile(file,attr);
})();
*/
2 Likes

Just found this old post as I am setting up some Hazel automations - very helpful - thank you

You’re welcome. There’s a newer one on a similar topic, too:

And I just noticed that the whose call is a bit over-engineered.
var db = app.databases[name];
should do the trick just fine and is a lot clearer.

1 Like

Hi all

looking over the script im a bit confused. where do you define the devonthink groups UUID in the script?

thx!

Z

I don’t. I define the database and the group name at the top (groupSelector). Better legibility than UUIDs.

thx @chrillek !

im trying to follow and even tried with chatgpt yet im really unexperienced with programming.

all im trying to do basically is move a single file (pdf) that is matched in hazel to a specific group in devonthink.

i see there are 2 groups defined in your script. i cut that down to 1 (is that correct) and added that script into hazel but the file does not seem to move over to the devonthink database
what am i missing here?

here is the modified script

function hazelProcessFile(theFile, inputAttributes) {

  var groupSelector = { 
               "1234": ["00-99 skm", "tax returns"],

/* add more lines as needed */
			   };
  var app = Application('DEVONthink 3');
  app.includeStandardAdditions = true;

  var groupSel = inputAttributes[0];
 
  var dbName = groupSelector[groupSel][0];
  var groupName = groupSelector[groupSel][1];
  var db = app.databases.whose({name: dbName});
  var group = app.search("name:" + groupName + " kind:group kind:!tag", {in: db})[0];
  app.import(theFile.toString(),  {to: group});
}

thx alot!

Z

Why are you trying to use Hazel for this (besides it being the topic of the thread)?

1 Like

thx @BLUEFROG , yeah i already have literally tens of these automations for other uses in devonthink itself :slight_smile:

I dunno really, I just thought it would be cleaner that way to move hazel automations directly with 1 process in hazel instead of 2 automations 1 in hazel and 1 in devonthink…1st world problems i guess haha

Z

Technically that is probably correct. But what’s the point to have a script that moves a single file to a pre-defined group?

hmm well i run different hazel actions for different pdf type (ie gas, electricity, phone bills) and i file each in a different devonthink group. does that make sense?

Also the script i pasted does not actually move the file for some reason

thx alot

Z

How is Hazel identifying each document type - by filename, contents, …?

i use somethings rules based on names and sometimes based on contents

I put these “rules” into my script; Hazel not needed

First off, it’s never a good idea to hijack an old thread. This one dates from 2020! Things have advanced since them.

The basic idea was (was!) to have Hazel forward information to the script. The script uses this information to select the correct database and group to send the files to. In the original post, the information was 1234 and tag1, and the groups were group1 in database1 for 1234 and group2 in database2. The script (which was written to be run in Hazel!) provides the information in its inputAttributes array.

After all that, I proposed another script here

That one is run in DEVONthink, at it’s more or less what I still use today for many PDFs. No need for Hazel anymore. The basic approach is

  • check the PDF for OCR and apply OCR if necessary
  • check the text in the PDF for certain patterns (key)
  • These keys are associated with functions (handlers), databases, groups and possibly tags lists
  • The handlers can do whatever they want to the file (rename, set custom meta data value)
  • Tags on the file are set depending on the content of the tags list
  • The database/group are used to determine the location the file is moved to after processing

It all boils down to a single script to handle all regularly arriving PDFs (account statements, invoices, whatever merits automation). But the code needs customization and a lot of experimentation. And it’s certainly not meant for ChatGPT to “improve” on it.

1 Like

thx and apologies, didnt intend to hijack the old thread, will create a new thread

best

Z