Hazel and DevonThink - Sending/import PDF-files to DT

Hej there,

I’m trying to set up a system to have Hazel automatically move a file into a specific DT folder.
I am a newbie and know only a little bit about scripting. :confused:
I found this info (http://www.noodlesoft.com/forums/viewtopic.php?f=3&t=4694).
As described there i tried to modify that script for my environment, but i only get an unspecific error from hazel working with my modified script.
But i don´t know why.
I created a test-Database in DT.
I created a group called “Finanzen”
I want Hazel - with help of the script on matching the filename “Kontoauszug.pdf”- that the file comes to DT and was sorted into the Group “Finanzen”.
I do not get a file called log.txt.

The error message i get form hazel is: “Shell script failed. Error processing shellscript on “Kontoauszug.pdf””.

Can´t figure out, where my fault is… :open_mouth:

----------------- my modified script from the source i gave the link above -------


This script moves files of to DevonThink folders automatically

based on the $Matches variable.

It looks for a match in the PDF File Name, and moves the file to

the corresponding DevonTHINK Folder Name.

If it does not find a match, it copies the file to the Inbox.

In either case, it moves the file to the Backup folder

use this pattern to match files: PDFFileName|DevonTHINKFolderName

echo $matches

IFS=$’\n’ read -rd ‘’ -a MatchNames <<<"$Matches"

FullPath=echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"

echo date > ~/HazelAutomation/log.txt
echo “Looking in [$1] for:” > ~/HazelAutomation/log.txt

for i in “${MatchNames[@]}”
IFS=$’|’ read -rd ‘’ -a GroupNames <<<"${MatchNames[$j]}"

  FolderName=$(echo ${GroupNames[1]})  #use this to remove trailing new line character

  echo $j "["$MatchString"]" >> ~/HazelAutomation/log.txt
  echo "$1" | grep -q "$MatchString" 
  if [[ $greprc -eq 0 ]] ; then
     echo "Found:[$MatchString] in [$1] moving to [$FolderName]" >> ~/HazelAutomation/log.txt
     tell application id "com.devon-technologies.thinkpro2"\n
        set theDatabase to open database "/Volumes/LWD_20160523/02_DevonThink/test/test.dtBase2"\n
        set theGroup to create location "'$FolderName'" in theDatabase\n
        import "'$FullPath'" to theGroup\n
     end tell\n
     echo $Command  >> ~/HazelAutomation/log.txt
     echo $Command | osascript
     mv "$FullPath" ~/HazelAutomation/4.HazelBackup/.
  let "j++"

echo “No match found for $1” >> ~/HazelAutomation/log.txt
cp “$FullPath” “/Users/rolf/Library/Application Support/DEVONthink Pro 2/Inbox/.”
mv “$FullPath” ~/HazelAutomation/4.HazelBackup/.

--------- End of script -------------

I would appreciate any suggestions. Thanks!

First – the Noodlesoft Forum link you posted is not a valid link. I didn’t find any over at the Noodlesoft forum with your user name. Perhaps you could repost the link here?

If Hazel is throwing an error, then the first place to look for help is the Noodlesoft forum.

The shell script you posted references several locations on someone’s local machine – are they addresses that you entered or address you obtained from a sample script? If they are not yours, then the script will probably fail. But, first check with the Hazel forum – Paul Kim and the other experts there will probably be able to set you on the right path.

(BTW, if you search this forum, you might find some other examples of working with Hazel and DEVONthink to achieve the same goal. For example, there are folks who have Hazel put files into folders that have folder actions - i.e., scripts - attached to them, and those folder actions do the importing for you. Check it out.)

Hello korm,
thanks for your help and your hint to the link.
At now it is corrected… Sorry for that.

Hmm… I have to check my script again to see what you mean with “different locations on local machines…” I didn´t recognized that…

So i will give it a revision… :open_mouth:

Thanks so far…

Best regards

Thanks for the link.

Just a quick glance at the script posted by @baandab in the Noodlesoft Forum post – and the script you posted above, there are several differences apart from the obvious changes you made to customize paths and matches to your circumstances. Perhaps you deleted important chunks of the script and thus modified the logic?

You might want to start over and modify a new copy of @baandab’s script?

I would also strongly suggest using a less terse scripting method, especially if you’re a beginner. DEVONthink’s Applescript dictionary is very capable and much easier to work with, even somewhat intuitively.

Hello Bluefrog,
thanks for your words… but please, do you know a post (here or where ever) which do it similar to what i want or searching for… Didn’t find such ones.
Please, if you have a link would you be so kind to post it for me? Maybe i have the wrong search words…
Best regards

Here is a simplified version of the script I use to import items into DEVONthink via Hazel:

property myUUID : "BD18D571-BD3D-4B27-BFDF-67DFCF933DAE" -- group Haushalt/Haushaltsgeräte/Entfeuchter

tell application id "com.devon-technologies.thinkpro2"
	set myGroup to get record with uuid myUUID
	set myRecord to import (POSIX path of theFile) to myGroup
	open window for record myRecord
end tell

The first line sets the destination group. I chose to directly embed the UUID. To make adopting the script easier for different groups I created a helper script that puts the UUID of the currently selected group into the clipboard ready to be pasted in the previous script.

tell application "DEVONthink Pro"
	set theSelection to selection
	set selectedDocument to item 1 of theSelection
	set myPath to name of database of selectedDocument
	tell selectedDocument
		set myUUID to uuid
		set myPath to myPath & location & name
	end tell
end tell

set the clipboard to "property myUUID : \"" & myUUID & "\" -- group " & myPath


I’ve been trying to find a script that I can use in my workflow (and do so with zero skills in Applescript). I want to use Hazel to move files (.eml) from a folder located in “My Documents” to a specific folder in my DTPO database.

I found this script from Korn (Hazel rule script for importing/indexing to a specific group) but haven’t been able to get it to work. I keep getting a script error. Does it need updating or am I doing something wrong?

tell application "Finder"
	set _path to (the POSIX path of theFile as string)
	set {_name, _extension} to {name, name extension} of theFile
end tell

tell application "System Events"
	if not (exists process "DEVONthink Pro Office") then
		tell application id "com.devon-technologies.thinkpro2" to activate
	end if
end tell

tell application id "com.devon-technologies.thinkpro2"
	set theGroup to get record with uuid "D26E06DA-ADB8-4AEE-AB53-AA7B95359AF0"
	set theImport to import _path name _name to theGroup
end tell

Hi Nate. Looks like there are some good suggestions in this thread already – did you try Sven’s? But, I just wanted to suggest trying Automator, which you might find easier. It’s what I use, and it works nicely.

So: I have a bunch of Hazel workflows, for which the penultimate step is “Run Automater Workflow.” This then runs an automator workflow specific to the group I want to import the document into.

These automator workflows have two steps: “Set Current Group” and then “Add Items to Current Group”. To find these actions, just search for them by name in Automator. (Or search for “DEVONthink” to see all available actions – it’s pretty cool what you can do here!)

For the first step, Automator lets you select your group with a pretty user-friendly interface, so you don’t have to mess with UUIDs. (The downside is that you may have to tweak the workflow if you move or rename the group. But it’s always obvious when this has happened – I’ve encountered no silent failures – and it’s easy to fix.)

For the second step, Automator lets you decide whether you want to import or index the document. (I import.)

There are various ways to save your Automator worklow – I use the “workflow” option, which works well. Then just point Hazel to this workflow. I keep them all together in a folder.

I also have a final step in the Hazel workflow to move the document to the trash. This always happens after the document has been imported to DTPO.

If I want to create a new workflow for a new class of document, I’ll usually duplicate an existing Hazel/Automator workflow and edit it to fit the new circumstances, so that I don’t have to remember the details of setup.

Give it a try!

What script error? :question:

It’s “Korm”

Ugh. This is the danger of posting scripts that solved someone’s specific problem and then another reader believes it applies universally. That script was written for a very specific UUID and is not universal for anyone who copies it – and therefore it fails. Ignore that other thread. When I get a chance I will delete everything I wrote there. The example above in this thread isn’t elegant, but it works.


Apologies for misspelling your name…

When I brought the script over from that older post, I did insert my own specific UUID, that of the target folder or whatever. I’ve been experimenting with a number of different scripts grabbed from here and there to develop a certain workflow. From my research in these forums, I felt confident that I could find the right pieces and build an “email” pipeline from Mail to DTPO. But what seemed doable has been much harder than I thought.

Goal: Create a workflow from Applemail that separates out my work mail and sends it into a specific folder in my DTPO database named “Email.” The key to this workflow is that it be automated.

So far, it mostly works. I found a nice script that exports my mail in bulk from one folder in Mail to another folder on the desktop. From that folder, Hazel sends the mail on to another with a rule that tags each email with either “Work Archive” or “Home Archive.” I can separate these because my work mail comes from a different account than the others. Anyway, it’s really only the last step or so of this that has given trouble, importing into specific DTPO folders and then deleting the source files from the two Finder Folders.

I’ve tried using 2 or 3 different Applescripts within Hazel but, like I mentioned, I’m trying to do this on the fly without any prior experience with scripting (or Hazel) so it’s hard for me to troubleshoot when they don’t work, though I’ve tinkered. I’ve tried scripts within Automator, i.e., the Devonthink Automator options which worked when I first tried, but then stopped working after I added “Move Finder Items to Trash.” All the items moved were moved to the trash before the DTPO import options could complete their work. Then I tried to incorporate a workflow pause with the trash action in a separate Automator workflow, but it refused to work. After that, I couldn’t get the original DTPO Automator actions to work again. Its baffling to me. I deleted these, rebooted, tried again…I’ve tried to use only the Folder Actions in Automator (without Hazel), putting together a workflow using the two key actions, “Set Current Group” and then "Add Items to Current Group.” But I continually get an error message at the very end which states that the current DTPO window isn’t open even while it actually is open…(and shouldn’t need to be).

dgbeecher- Thanks for the encouragement and suggestions to use Automator. I wonder if I could lean on you to share the exact sequence/workflow of the part which sends your files into DTPO. Then I could compare it to what I’ve already tried.

Comment: My workflow has has a Rube Goldberg-esque feel to it, a contraption that has a bit too many levers and pulleys for what it actually accomplishes. Its also taken far too much of my time, rummaging around everywhere for parts and enough instructions to put something together. Would it be helpful for Devonthink & Co. to facilitate an exchange on the website in addition to the forum? It could be a spot where user scripts & workflows can be easily referenced, shared, and tinkered with. :slight_smile:

Hi Nate,

Here are a couple images of the workflows I use.

First, the Hazel workflow (just the action part, since the conditions must of course be specific to your own use case, and shouldn’t affect what actually happens to the file):

And here’s an example Automator workflow. This is the entire thing.

Note that I have Hazel send the file to the trash, not Automator. (Seems you had bad luck trying it the other way around.)

This set-up is not particularly elegant (as Korm noted), but it has worked without a hitch for me, for probably 2+ years. If DTPO is not open, Automator opens it up and imports the file. If DTPO is already open, Automator imports the file and DTPO shows it to me in a new window.

I don’t have insight into the errors you’ve been seeing with your approach, but I hope these simple examples help you fix them.

I thought I’d also report that I regularly use the script you linked to above – from Korm’s 7/10/2013 post here – and which you said was giving you an error.

I have Hazel watch a folder (into which I have Keyboard Maestro drop text files as part of a complicated PDF note-taking workflow, that I may describe on these forums someday…). Hazel then runs the script, via this rule that is probably so simple it’s not worth posting a picture of:

I just tried dropping an email from macOS Mail into this folder, and that got imported no problem to the specified group in DTPO.

So … all is good on this computer … sorry that’s probably not much help to you!

P.S. to Korm: Please don’t delete that post like you warned you would! It was super useful to me. Thanks for that, and for the many other tips you’ve provided on these forums.

Thanks a million!

I noticed that I hadn’t been using the “embedded” option in Hazel so when I tried that script again it worked perfectly! So now I’m home free except for a bit of streamlining on the Mail side of things.

Funny, my automator setup was the same as you have pictured…so I’m not sure why it wasn’t working for me. But I’m pleased with the script and will use that instead- at least for now.

I’ve used DTPO for many years now but this is only my first time digging in and using scripts to automate. I’m excited for my new workflow :slight_smile: Thanks!

Hey, I’m super happy it finally worked for you! Strange that embedding the script should make any difference…

Now that you’ve had me thinking about this, I may actually switch all my automator workflows over to applescript. it is the simpler, more robust method.

I may also start archiving some of my emails to DTPO, as you do. I’ve had to switch from gmail to Apple Mail recently, and am disappointed with its search capabilities.

When I ran my previous Automator workflow, it got it trouble with what seemed to be timing and sequence. It would start trashing my files (last step) before the DTPO import was complete (first step). I must have been doing something wrong with it.

I’ve only just now started to use DTPO to archive and search email. If my memory serves me, I started using DTPO back in 2005, years ago, but never did much with the emails. Partly, it seemed like it was too complicated, too many steps. A bigger reason was that my email volume was manageable. Things have now changed for me.

My work has shifted and I’m anticipating that the volume will not only increase but my need to manage it will be far greater than it was before. The “new” wisdom that’s out there in the podcast world is suggesting that email is most effectively processed through big bulk files with deep search functionality rather than maintaining a myriad of independent files used for sorting. So I use (learning) Omnifocus for my task manager, sending “to do” emails directly to that inbox. Then any home or work emails that need to be saved get send on my processing pipeline to DTPO. I simply have two files in my DTPO Email database, Home Files, and Work Files. The goal is to use the search for anything that I need. The other goal is that emails are touched once and the process is automated. I’m almost there but still need a few tweaks within Mail itself.

But this is an experiment. My fears have already come true in that the upfront cost of setting this up has been high. I feel like I’ve been putting together a science fair project on my garage workbench, spending time rummaging around the junk drawer for nuts and bolts and…duck tape, of course. To do this I gave up working in Airmail and migrated back to Apple Mail because there are better DTPO connectors. I miss Airmail. Part of the time issue has been my lack of experience and skill in navigating this forum and finding what I need quickly. Part of it has been my lack of experience and knowledge about scripts.

Hopefully, you’ve have a smoother setup :smiley: I am already seeing some payoff from this so that is good. - Nate