Keep only the latest version of the file upon import

I have setup a Hazel rule to watch for some files and import them into DT if there are any changes.
The Hazel rule watches a specific folder with the condition:

Date last modified is after Date last matched

And upon this runs an AppleScript

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 "6671AFC6-006C-4702-95CD-D4D139B7ABE6"
	set theImport to import _path name _name to theGroup
end tell

Everything works just fine and modified files get imported into DT but the problem is that every time I modify a file a new version of that file gets created in the DT. Which I guess is correct, since the file has different content, be it even a single character.

So the question is:
How can I tell DT to just replace the file if it has the same name, since I want to keep only the last version of the file in the DT database?

A good effort but a few things…

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

Why are you using this?

  1. The name will come in with the import.
  2. Hazel should be passing the correct data to the parameter, theFile IIRC, so you don’t need to get the path explicity.

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

You don’t need to talk to System Events for this. A launch or activate will open DEVONthink if it’s not open and activate will bring it to the front.

tell application id “com.devon-technologies.thinkpro2”

The advocated construct is now DNtp, not com.devon-technologies.com.

Instead of using Hazel and a script, we don’t you just index this file?

1 Like

I thought that’s how I get the file I need, but you’re right of course!
I was able to refactor the script into this:

tell application id "DNtp"
    set theGroup to get record with uuid "6671AFC6-006C-4702-95CD-D4D139B7ABE6"
    set newFile to import theFile to theGroup
end tell

I want to keep the file itself in the database, so I can have access to file’s content in the DTTG.
Or is my understanding of “Indexing” a file incorrect? (as it seems so after reading the forums).
I thought “indexing” is just creating a “shortcut” to a file without really “bringing” the content into DT. But upon reading the forums it seems like “indexing” is similar to a hardlink (which is even better, since I can edit the file both in DT and external editor and changes are reflected everywhere)

I thought “indexing” is just creating a “shortcut” to a file without really “bringing” the content into DT.

It doesn’t copy the file, but it indexes the contents (if possible) for search and AI functions.

Indexing is definitely not like hardlinking. In fact, it’s closer to symlinking (especially since it breaks when files are moved, instead of tracked like aliases in the Finder).

You hadn’t mentioned DEVONthink To Go. You can still index into DEVONthink databases. DEVONthink To Go does not support indexing, but the contents of indexed items are synchronized by default. This means DTTG has access to the contents when syncing.

1 Like