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