Script (applescript/JS) to send a file from Hazel to a specific devonthink group based on uuid?

hi all, I originally replied to an old thread

https://discourse.devontechnologies.com/t/import-from-hazel-to-various-groups-javascript/55635/13

yet it seemed the OP suggested this wasnt exactly relevant im posting a new thread on this

i am wondering if anyone has done something like this in the past and may be willing to share his script? im trying to have a last step in hazel that will directly send a matched and then proccesed file to a specific devonthink group based on uuid.

thanks so much in advance

Z

I use the following - your choice as to whether to use group name or UUID – just comment out (or delete) whichever you aren’t using:

	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 3") then
		tell application id "DNtp" to activate
	end if
end tell

-- Import the file.
tell application id "DNtp"
	--set img_ib to get record at "/Investments" in database "FileCabinet"
	set img_ib to get record with uuid "893B5B25-C929-4659-BA14-E6345B53196A"
	import theFile to img_ib
end tell

I’m sorry if this response is not helpful to you but I thought it might be worth mentioning the way I prefer to use Hazel with DEVONthink.

I use Hazel simply to route files to the DEVONthink inbox (so that they’re then automatically picked up by DEVONthink) and then rely on various smart rules in DEVONthink automatically to place the relevant file in the appropriate group in DEVONthink. I do find that works well for me but appreciate it may not for you.

Stephen

5 Likes

BTW, it is not necessary to use Hazel for it. One may use system feature called Folder actions and attach AppleScript to the folder

1 Like

interesting!

@Silverstone any chance you have an example script that you dont mind sharing?

best

Z

While folder actions are still viable, in most cases they have been superseded by DEVONthink’s smart rules.

3 Likes

Like this?

-- Script to move saved file to DT
-- Created by Silverstone on 14.04.2021
-- Updated on 26.04.2021 to escape working on MS Office TEMP files

on adding folder items to this_folder after receiving added_items
	set added_items to set_viable_list out of added_items
	try
		if (count of added_items) is greater than 0 then
			tell application id "DNtp"
				try
					set theGroup to (get selection)
					if theGroup is {} then
						set theGroup to display group selector
					else
						set theGroup to item 1 of theGroup
					end if
					if kind of theGroup is not "group" then set theGroup to current group
					set theImported to 0
					repeat with theItem in added_items
						try
							set thePath to POSIX path of theItem
							set theRecord to import thePath to theGroup
							perform smart rule trigger import event record theRecord
							if exists theRecord then do shell script "rm -R " & quoted form of thePath
							log message info "Item '" & proposed filename of theRecord & "' is imported successfully to '" & name of theGroup & "'" record theRecord
							set theImported to theImported + 1
						end try
					end repeat
					activate
				on error ErrMsg
					tell application "Finder" to set theName to name of the theItem
					log message "There was a problem importing a file" info "Importing of file '" & theName & "' into the group '" & name of theGroup & "' is failed. Error: " & ErrMsg
				end try
			end tell
		end if
	end try
end adding folder items to

on set_viable_list out of theList
	set theClearList to {}
	repeat with theItem in theList
		set thePath to POSIX path of theItem
		tell application "Finder" to set theName to name of the theItem
		if thePath does not end with ".download:" and thePath does not end with ".crdownload:" then
			if theName does not start with "~$" then
				set the end of theClearList to theItem
			end if
		end if
	end repeat
	return theClearList
end set_viable_list
1 Like

PS: DEVONthink already comes with some built-in folder action scripts. This is discussed in the Automation > Folder Actions section of the built-in Help and manual

Bear in mind, folder actions are run by the Folder Actions Dispatcher, a separate process controlled by the OS, not us.

1 Like

thx both!

will look into this after the holiday break

happy holiday all!

Z

You and yours enjoy a safe and happy year’s end!