script to import a file from the finder

I need an AppleScript or a workflow that can be used with Hazel to import a file from a specified folder (specified maybe in Hazel) into a particular DTPO database and then delete it from the folder.

The idea is that any file sent to a Dropbox folder will cause Hazel to run a script that will send that file to the DTPO database.

Is there a template I can work on?

There are several posts on the forums about this topic: DEVONthink, Hazel and Automator

The database has to be open ?

Yes - and do NOT start trying to add files into the internals of a DEVONthink database or you will cause inconsistencies in the database.

Heaven forbid that I should poke around in the internals.
I want a share from iOS that just goes to DTPO. I was trying circuitously. Thing is, if I use any share sheet in iOS the iOS app has to be opened, too, to send it to a Dropbox-synced database. I wanted to send and forget, then see it on macOS.
DTPO doesn’t do as well as Keep It in this regard.

Correct.

Keep It is a very different application technologically.

You’re right.

This thread:

… includes a Hazel script that imports to a group whether its database is open or not. Can’t that be applied to a Finder selection using an Automator workflow ?

I’m not sure where you’re getting this idea.

From Korm, here.

I was looking for a way to create a service from an Automator workflow which sends files to a group, to which I could set a keyboard shortcut.

I know there’s a bundled service in DTPO that sends files to the global inbox that I could use for this, but I wanted rather to send files to a specific group - and to do that whether the group’s database is open or not - or indeed whether DTPO is open or not.

That’s sort of what the script for the Hazel rule in that other thread does, only it’s not set up to be used as an Automator service for selecting files in the Finder. I thought maybe it could be adapted so that it could be.

I think I did it with this:

tell application "Finder"
	set itemlist to the selection
	set theFile to (item 1 of itemlist) as alias
	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 "INSERT_UUID_HERE"
	
	-- 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

This can be used to place in an Automator worflow and then used as a service to which a keyboard shortcut can be assigned.

… BUT it only handles single files.

… can anyone adapt it for multiple files?

1 Like

This clearly tells DEVONthink to open. This is not adding files without DEVONthink or the appropriate database being open.

If I close the target database, quit DTPO, and then use that service on a file on the desktop, DTPO opens, the target database opens, and the file is added.

I just tested it again. It works.

It doesn’t delete the file - that’s left on the desktop or wherever, but it’s copied into the right database.

Just to be clear - this is no longer using Hazel … this is just using that script in an Automator workflow saved as a service, and evoked via a contextual menu click on a file (which can have a keyboard shortcut linked to it).

Correct. I thought you wanted it to work without opening DEVONthink (which again, is not possible or it’s unsafe for your databases).

Regarding deleting the original in the Finder, add this line after the import command’s line and it should delete the item…


if exists theImport then tell application "Finder" to delete _path

Ah, we were at cross purposes.

I can’t get your delete line to work, though. Keep getting “Finder got an error: Handler can’t handle objects of this class” …

Change the variable in that line to…


(_path as posix file)

That works. Thank you !

You’re welcome. :smiley: