Hazel rule script for importing/indexing to a specific group

Here is a revised version of the Hazel import script that will find the destination group whether or not the database is open, and even if the group has been moved to another location with a database or moved to another database.

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 "<group uuid>"
	
	-- 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

To set up this version, you need to group’s UUID. To obtain that, click on the target group in DEVONthink being sure to select the group itself and not something inside the group. Then, from the contextual menu, select Copy Item Link. Paste the clipboard contents (the item link) into the script here


set theGroup to get record with uuid "x-devonthink-item://A4BE6367-73A8-41D3-8538-75DCB9D6929C"

edit that to remove the string “x-devonthink-item://” so that the command is now


set theGroup to get record with uuid "A4BE6367-73A8-41D3-8538-75DCB9D6929C"

Much gratitude to Christian for giving me the tip that led to this much more flexible version of the script.