Adding to the Import, OCR & Delete script

Hi Guys

Got the Import, OCR & Delete script working but it only pops the file into the Global inbox.

Is there another layer of scripting that you could help me with that would then move the file to a group in a specific database?

Thank you!

The easiest way is to just switch Preferences > Import > Desintation to Select group.

Hi Bluefrog,

Thank you for your reply.

The folder that uses that script is only being used for receipts.

What I am hoping to do is drop all of my receipts into the folder, have them OCR’d and “filed away” - all without having to do anything manually in DTPO.

I was thinking that tagging may also work - but I wanted some input from the experts.

You can change the tell block for DEVONthink like this…

tell application id "DNtp"
	if not (exists (database "myDatabase")) then -- Is the database open?
		set theDatabase to open database "/path/to/myDatabase.dtBase2" -- Open it, if not.
	else
		set theDatabase to database "myDatabase.dtBase2"
	end if
							
set theGroup to get record at "/location/of/group" in theDatabase -- Get the location of the destination group in the database.
							
set theRecord to ocr file thePath to theGroup
if exists theRecord then tell application "Finder" to delete theItem
end tell
```… obviously change the path and location info to match your environment.

That worked perfectly, thank you for helping!

You’re welcome.

Hi Bluefrog,

Wondering if you would know how to change the script so that it will check for any other files in the folder?

I have found that if you drop more than one file into the folder, the script only processes the first file and ignores the rest.

This is not controlled by the script. Folder Actions trigger on their own and by their own controller (Folder Actions Dispatcher).

I suggest detaching the Folder Action, removing any files in the folder, then reattaching it. Then try adding the files again.
Also, if you are saving to the folder, this may not work correctly as the file could be detected before the save is completed. Moving a file into the folder would be more likely to behave.

FWIW Here’s a modification of the Import, OCR & Delete script that

  1. Imports to the location of your choice (see the properties at the start)
  2. Runs OCR on PDFs and does a simple import on non-PDF documents

Finally, you can test it by running from Script Editor as well. Seems to work well as a Folder Action script.


-- DEVONthink - Import, OCR & Delete.applescript
-- Created by Christian Grunenberg on Fri Jun 18 2010.
-- Copyright (c) 2010-2017. All rights reserved.
-- dbyler update: files to a specific location (h/t https://discourse.devontechnologies.com/t/adding-to-the-import-ocr-delete-script/24777/1) and support non-PDF docs

property myDatabase : "Admin"
property myDatabasePath : "/Users/YOURNAME/Documents/DEVONthink Databases/" & myDatabase & ".dtBase2"

on run
	set mySelection to (choose file with multiple selections allowed)
	my main(mySelection)
end run

on adding folder items to this_folder after receiving added_items
	my main(added_items)
end adding folder items to

on main(added_items)
	try
		if (count of added_items) is greater than 0 then
			tell application id "DNtp" to «constant rmdara24»
			repeat with theItem in added_items
				set thePath to theItem as text
				set myOcr to (thePath ends with ".pdf")
				if thePath does not end with ".download:" and thePath does not end with ".crdownload:" then
					set lastFileSize to 0
					set currentFileSize to 1
					repeat while lastFileSize ≠ currentFileSize
						delay 0.5
						set lastFileSize to currentFileSize
						set currentFileSize to size of (info for theItem)
					end repeat
					
					try
						tell application id "DNtp"
							
							if not (exists (database myDatabase)) then -- Is the database open?
								set theDatabase to open database myDatabasePath -- Open it, if not.
							else
								set theDatabase to database myDatabase
							end if
							
							set theGroup to get record at "/Inbox" in theDatabase -- Get the location of the destination group in the database.
							
							if myOcr then
								display notification "Importing to DT (OCR)"
								set theRecord to ocr file thePath to theGroup
							else
								display notification "Importing to DT"
								set theRecord to import thePath to theGroup
							end if
							if exists theRecord then tell application "Finder" to delete theItem
							
						end tell
					end try
				end if
			end repeat
		end if
	end try
end main

3 Likes

Nice work! Thanks for sharing your modification.

Thank you for sharing @dbyler

Wow, I have known I’m seriously under-utilizing DEVONthink for about three years, but just haven’t had time to really dig into some of the scripting stuff. So today I had a little time and I decided to search the forum for an answer – and here I find the solution to my exact problem posted just two weeks ago. @dbyler, thank you! Amazing what you can find when you search around for some help.

Sorry. I’ve been away from scripting for a while and guess I’m in the weeds. In…

property myDatabase : “Admin”
property myDatabasePath : “/Users/YOURNAME/Documents/DEVONthink Databases/” & myDatabase & “.dtBase2”

I’ve changed “Admin” to “MYDATABASENAME”
I changed YOURNAME to MYNAME.
I made sure the path to my database is correct and that the database is open in DT3.

I ran the script two ways: selecting the item to import, and as a folder action.
The selected item was deleted but not imported to my database. The folder action also deleted but didn’t import.

I didn’t find the name of the item to be imported anywhere in my database.

My goal is to have the item imported into whatever my currently open and showing group is. I don’t think this script goes that far but I can’t seem to get it to work at all.

This is so good! Exactly what I have been looking for since a long time…
Finally it is working now!
Thank you so much!

What is the difference in using this script as opposed to a smart rule that applies OCR upon import?