# Import from Hazel to various groups - JavaScript

**URL:** <https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635>\
**Category:** Automation\
**Created:** [May 13, 2020, 3:32pm UTC](https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635 "2020-05-13T15:32:05Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [May 13, 2020, 3:32pm UTC](https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635/1 "2020-05-13T15:32:05Z")

</div>

There’s a fairly old AppleScript importing from Hazel to a fixed DT group [here](https://discourse.devontechnologies.com/t/hazel-rule-script-for-importing-indexing-to-a-specific-group/15941). 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.

```auto
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);
})();
*/

```

---

<div class="post-metadata">

**Author:** ![rkaplan](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/r/a3d4f5/32.png) [@rkaplan](https://discourse.devontechnologies.com/u/rkaplan)\
**Post date:** [January 18, 2023, 5:05pm UTC](https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635/2 "2023-01-18T17:05:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [January 18, 2023, 5:29pm UTC](https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635/3 "2023-01-18T17:29:17Z")

</div>

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

> [@Using a single smart rule instead of Hazel (maybe)](https://discourse.devontechnologies.com/t/using-a-single-smart-rule-instead-of-hazel-maybe/67982/):
>
> Some people (me included) use a naming convention for their files and put them in certain goups depending on sender and/or content. I’m doing that for bank statements, some of the regularly arriving invoices and income statements. In DT, you’d need one smart rule per target group, since it’s not possible to move a record to a group depending on some criteria. So if you have 5 bank accounts and two companies regularly sending you invoices, you’d need seven separate rules. Which is not good, beca…

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.

---

<div class="post-metadata">

**Author:** ![zeltak](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/z/a88e4f/32.png) [@zeltak](https://discourse.devontechnologies.com/u/zeltak)\
**Post date:** [December 17, 2024, 1:21am UTC](https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635/4 "2024-12-17T01:21:32Z")

</div>

Hi all

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

thx!

Z

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [December 17, 2024, 6:21am UTC](https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635/5 "2024-12-17T06:21:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zeltak](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/z/a88e4f/32.png) [@zeltak](https://discourse.devontechnologies.com/u/zeltak)\
**Post date:** [December 18, 2024, 3:12pm UTC](https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635/6 "2024-12-18T15:12:07Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [December 18, 2024, 3:34pm UTC](https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635/7 "2024-12-18T15:34:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zeltak](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/z/a88e4f/32.png) [@zeltak](https://discourse.devontechnologies.com/u/zeltak)\
**Post date:** [December 18, 2024, 3:55pm UTC](https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635/8 "2024-12-18T15:55:14Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [December 18, 2024, 3:56pm UTC](https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635/9 "2024-12-18T15:56:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zeltak](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/z/a88e4f/32.png) [@zeltak](https://discourse.devontechnologies.com/u/zeltak)\
**Post date:** [December 18, 2024, 3:58pm UTC](https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635/10 "2024-12-18T15:58:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BLUEFROG](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/bluefrog/32/135_2.png) [@BLUEFROG](https://discourse.devontechnologies.com/u/BLUEFROG)\
**Post date:** [December 18, 2024, 4:02pm UTC](https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635/11 "2024-12-18T16:02:12Z")

</div>

> [@zeltak](#):
>
> i run different hazel actions for different pdf type (ie gas, electricity, phone bills)

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

---

<div class="post-metadata">

**Author:** ![zeltak](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/z/a88e4f/32.png) [@zeltak](https://discourse.devontechnologies.com/u/zeltak)\
**Post date:** [December 18, 2024, 5:06pm UTC](https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635/12 "2024-12-18T17:06:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DTLow](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/dtlow/32/18161_2.png) [@DTLow](https://discourse.devontechnologies.com/u/DTLow)\
**Post date:** [December 18, 2024, 5:18pm UTC](https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635/13 "2024-12-18T17:18:34Z")

</div>

> [@zeltak](#):
>
> somethings rules based on names and sometimes based on contents

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

---

<div class="post-metadata">

**Author:** ![chrillek](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/chrillek/32/29340_2.png) [@chrillek](https://discourse.devontechnologies.com/u/chrillek)\
**Post date:** [December 19, 2024, 8:47am UTC](https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635/14 "2024-12-19T08:47:35Z")

</div>

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

> [@Using a single smart rule instead of Hazel (maybe)](https://discourse.devontechnologies.com/t/using-a-single-smart-rule-instead-of-hazel-maybe/67982/1):
>
> Some people (me included) use a naming convention for their files and put them in certain goups depending on sender and/or content. I’m doing that for bank statements, some of the regularly arriving invoices and income statements. In DT, you’d need one smart rule per target group, since it’s not possible to move a record to a group depending on some criteria. So if you have 5 bank accounts and two companies regularly sending you invoices, you’d need seven separate rules. Which is not good, beca…

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.

---

<div class="post-metadata">

**Author:** ![zeltak](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/z/a88e4f/32.png) [@zeltak](https://discourse.devontechnologies.com/u/zeltak)\
**Post date:** [December 19, 2024, 11:22pm UTC](https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635/15 "2024-12-19T23:22:20Z")

</div>

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

best

Z
