Help with Moving from Import, OCR, & Delete Script > Smart Rule

Hi all,

I’ve been using the Import, OCR, & Delete Script for a while. After recently upgrading to Devonthink 3, I noticed it wasn’t working anymore. I started making changes to the script, but then came across the recommendation in the Take Control Boo to use a smart rule. I’ve got my folder indexed, but I’m wondering how to

  1. Rename the file on import - I’m using the import from iPhone feature in the folder right click menu to bring in items that can go through my ScanSnap, and they all get saved as “Scanned Document.pdf”.
  2. Choose the destination group, which is something I had set up in the old script.

Are these two things possible to do? I’m having trouble figuring it out in the smart rules creation dialog.

How should the file actually be renamed, e.g. look for certain strings or dates?

Was this always the same destination group?

Hey Christian, thanks for the reply.

For 1, I’d just like to have a dialog come up and type in the name - manually typing it is fine, I just want it to be part of the workflow.

For 2, I’d like to mimic or use the functionality of Select group, as in Preferences > Destination > Select group, which is how things are set up for my ScanSnap right now. I think that’s how I had your script set up, though I might be wrong, and testing it back on DT 2, it’s not working. Here’s what I had:

-- DEVONthink - Import, OCR & Delete.applescript
-- Created by Christian Grunenberg on Fri Jun 18 2010.
-- Copyright (c) 2010-2017. All rights reserved.

on adding folder items to this_folder after receiving added_items
	try
		if (count of added_items) is greater than 0 then
			tell application id "DNtp" to launch
			repeat with theItem in added_items
				set thePath to theItem as text
				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"
							set theRecord to ocr file thePath to incoming group
							if exists theRecord then tell application "Finder" to delete theItem
						end tell
					end try
				end if
			end repeat
		end if
	end try
end adding folder items to

Smart rules don’t support user input, they’re supposed to run silently in the background (at least without scripting). However, version 3 includes the same script which can be used in the Finder’s folder actions. Finally, just select Preferences > Import > Destination > Select group and the group selector should be used.

Thanks @cgrunenberg. The script for
DEVONthink - Import, OCR & Delete.scpt
in
~/Library/Scripts/Folder Actions Scripts
seems to be the same script as before installing DT3, even after running install add ons from DT3. Should it have a different app name in the script file? If it still says “DNtp” is this evidence that it’s the old script? The text is exactly the same as what I posted above.

Thanks,
Sam

Both versions use this to ensure compatibility of scripts to multiple editions/versions. You could remove the installed folder action scripts and install them again to be sure that the latest one is really used.

So I’ve removed the old scripts, reinstalled the new scripts, checked that the folder action is set up, and restarted my computer for good measure. The script works mostly: I can drop a file in the folder, the folder action runs and grabs the file, I can see Devonthink process it and OCR it, and it appears in Devonthink, but it gets dumped in the global inbox, rather than allowing prompting me for the destination. The select group preference doesn’t seem to be applied. Here’s a screenshot of the settings in Devonthink:


and here’s a screenshot of the script:

What else am I missing to get the select group prompt to run?

Remove this…

Perfect, thank you!

Is there a way to allow a manual rename on the import as well?

Yes it’s possible, however automated processes like folder actions typically run headlessly with no user intervention.

That being said, this does what you requested but note it will switch to DEVONthink automatically for the rename.

try
    tell application id "DNtp"
        set theName to ""
        activate -- DEVONthink is going to need focus so we're explicitly giving it focus.
        set currentName to (do shell script "basename " & (quoted form of (POSIX path of (theItem as text))) & " | sed -E 's_(.*)\\..*$_\\1_'")
        repeat while theName = "" -- This just safeguards against an empty filename
            set theName to text returned of (display dialog "Enter the filename:" default answer (currentName as string)) -- Using the current filename also safeguards against an empty filename
        end repeat
        set theRecord to ocr file thePath --to incoming group
        set name of theRecord to (theName & ".pdf" as string)
        if exists theRecord then tell application "Finder" to delete theItem
    end tell
end try

Also note this is using more advanced techniques to cull the current filename as a default filename for the output. There are other methods in pure AppleScript that would take a bit more effort to get the same result.

Thanks @BLUEFROG. I’m having a bit more trouble with this that I’d like to admit. If I replace the tell - try block in the previous script so that it looks like this:

on adding folder items to this_folder after receiving added_items
	try
		if (count of added_items) is greater than 0 then
			tell application id "DNtp"
				set theName to ""
				activate -- DEVONthink is going to need focus so we're explicitly giving it focus.
				set currentName to (do shell script "basename " & (quoted form of (POSIX path of (theItem as text))) & " | sed -E 's_(.*)\\..*$_\\1_'")
				repeat while theName = "" -- This just safeguards against an empty filename
					set theName to text returned of (display dialog "Enter the filename:" default answer (currentName as string)) -- Using the current filename also safeguards against an empty filename
				end repeat
				set theRecord to ocr file thePath --to incoming group
				set name of theRecord to (theName & ".pdf" as string)
				if exists theRecord then tell application "Finder" to delete theItem
			end tell
		end if
	end try
end adding folder items to

then adding a file to folder will kick off the script (I see a gear icon in the menu bar), switch to Devonthink, and then stop. Nothing is processed, no name is asked for.

If I run your block as it’s own script, nothing happens. Did I miss a few parts pasting it in?

Thanks.

This is the entire script…

on adding folder items to this_folder after receiving added_items
	try
		if (count of added_items) is greater than 0 then
			tell application id "DNtp" to launch
			repeat with theItem in added_items
				set thePath to theItem as text
				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"
							activate -- DEVONthink is going to need focus so we're explicitly giving it focus.
							set theName to ""
							set currentName to (do shell script "basename " & (quoted form of (POSIX path of (theItem as text))) & " | sed -E 's_(.*)\\..*$_\\1_'")
							repeat while theName = ""
								set theName to text returned of (display dialog "Enter the filename:" default answer (currentName as string))
							end repeat
							set theRecord to ocr file thePath --to incoming group
							set name of theRecord to (theName & ".pdf" as string)
							if exists theRecord then tell application "Finder" to delete theItem
						end tell
					end try
				end if
			end repeat
		end if
	end try
end adding folder items to

You’re welcome :slight_smile:

Hey @BLUEFROG, so when I had been doing my testing, I already had DT3 open. But when I don’t have it open, and drop something in the folder, it opens DT2. Is there a way to correct that, other than to delete DT2?

You should not run DEVONthink 2 and 3 on the same account, on the same machine. At a minimum, you should delete DEVONthink 2.x from the /Applications folder and reboot the machine.