Hazel rule script for importing/indexing to a specific group

Thank you for posting the script.

A few ideas:

  1. The script is written to operate in Hazel. The value of the variable theFile in the second line of the script is provided by Hazel when Hazel executes the rule where the script is stored. It’s not a standalone script to be run inside DEVONthink or from ScriptEditor, etc. So, if you getting a path error, it’s possibly a Hazel issue, or because you tried to run the script outside of Hazel. (It would be unusual for Hazel to get a file path wrong.)
  2. The script will not work with DEVONthink 3 without changing two lines:
tell application id "com.devon-technologies.thinkpro2" to activate

should be

tell application id "DNtp" to activate

and

tell application id "com.devon-technologies.thinkpro2"

should be

tell application id "DNtp"
1 Like

Would you need to change “DEVONthink Pro Office” to “DEVONthink 3” as well?

Good catch. Yes :slight_smile:

And, of course, you need the UUID of the targeted group for the hardcoded destination that makes this script work.

set theGroup to get record with uuid "<group uuid>"

Generally, hardcoding destinations is “a bad idea” but in this case the fixed destination is the point of the script as it operates in Hazel, which itself frequently deals with fixed destinations.

1 Like

Thanks korm and mbbntu for your input.

I did originally run this from within Hazel and was having issues which then lead me to run it directly from script editor to try and troubleshoot. But as you’ve suggested I’ve reverted back to running within Hazel and I am getting the following errors appear:

The following is the exact script I have embedded into my hazel rule:

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 "D95C7EBA-FB02-43AA-9115-163425413560"
set theImport to import _path name _name to theGroup
end tell

I am still using DevonThink Office Pro 2.

And for completeness, below is the Hazel rule I have in place:

You’re going to need to ask NoodleSoft’s forum for advice on Hazel errors. The script works over here.

It worked for me when I tried it. You should perhaps look at this post above https://discourse.devontechnologies.com/t/hazel-rule-script-for-importing-indexing-to-a-specific-group/15941/14?u=mbbntu particularly the last few lines. I’ve no idea what “return true” might be doing, but the script with that has worked for me. Plus permissions are obviously important.

You might also want to add to your Hazel rule. You can make one of the conditions that the item is not labelled “Blue” (for example) and then make the last action of the rule that it applies the label “Blue” to the item. That prevents a loop, because anything that has been processed once is marked in such a way that it does not match the opening criteria. When you have things working you can make the final action the Trashing of the item.

I use the same sort of workflow successfully in Hazel and DTPO2. I checked my script, and it appears almost the same as yours. However, there are a few small differences, and I don’t know if there is a subtle difference that might help. Good luck!

    tell application "Finder"
	set _path to (the POSIX path of theFile as string)
	set {_name, _extension} to {name, name extension} of theFile
	
	-- optional: hide extension in Finder
	set extension hidden of theFile to true
	
	-- optional: remove extension for DEVONthink displays
	set _name to text 1 thru -((count _extension) + 2) of _name
	set _extension to "." & _extension
end tell

-- Launch DEVONthink if it isn't already open.
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

-- Import the file.

tell application id "com.devon-technologies.thinkpro2"
	set theGroup to get record with uuid "7A72BA27-C302-4362-8B25-01ACEC1C1F9A"
	
	-- choose one option, Index or Import, comment out the other
	set theImport to import _path name _name to theGroup
	-- set theIndex to indicate _path to theGroup
end tell

Just some side notes…

  • We advocate using tell application id "DNtp".
  • Also, this code block is unnecessary though not necessarily incorrect)…
-- Launch DEVONthink if it isn't already open.
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

Sending a command to DEVONthink will launch it.

  • Lastly, note the activate command will bring DEVONthink to the front.
    You can use launch to run a command without bringing the app to the front, though that’s actually not necessary here.
    launch is typically used to open an app without switching from your current one, but would be unneeded here. However, some apps may require it, as needed.

I was working with this script in a combination of hazel and DT3 for a while but at the moment the script is not moving the files into the desired groups anymore. Instead it moves the files into the inbox of the specific databases. To change the uuids doesn’t bring a difference since the script will just move the files into the inboxes.

I am wondering what I am doing wrong since the script was working well in DT3. Is it due to a recent update?

Copy this part into a new script in Script Editor and comment out the import line.

Does theGroup actually return anything other than missing value?

Close, but nope - You comment AppleScript with two dashes, as the other lines show.

That looks fine. I installed Hazel (as I don’t use it). Can you select the folder in Hazel and export the rule and send it to me? Thanks.

Hello Bluefrog,

thank you very much for your help. I am not sure what it was but the last files today were again sorted correctly into the specific group in hazel. I will come back if the flaw is occurring again and am very grateful for the support that you offer so well. :+1:t2:

1 Like

You’re very welcome and thank you for the kind words. It is very appreciated! :blush:

Cheers!

Hello ,

the input does not work for me :
tell application id “DNtp”

My path is:
tell application id “com.devon-technologies.think3”

Do not have DEVONthink 2 on the same account or machine.

tell application id "DNtp" is the correct form to use.

Quick update, managed to get this working on Catalina with a beta version of Hazel.

For anyone who wasn’t aware, this script is written specifically to be used with Hazel’s “embedded script” option, not as an external script.

I assume the handling of the file will be slightly different as an external script, but I wasn’t bothered enough to update the script to do so, choosing to save and sync it inside of my Hazel rules instead.

1 Like