This is my posted thread at MacScripter I moved to this Devonthink post. Since the issue at hand seems to more directly involve DT, I thought I might get better results–and ultimately help more people–here.
I am using an applescript and Lingon application (based on a MacTipper blog entry at this link http://www.mactipper.com/2008/04/use-launchd-instead-of-folder-actions.html) to integrate a Fujitsu ScanSnap scanner with DevonThink. (BTW, the script and Lingon app can be downloaded by following links on that blog entry.) In Lingon I’ve set up a daemon to run an applescript whenever a folder is modified. The workflow is: scan a document to a mac folder “Scan Folder”. The daemon “sees” that and triggers the applescript which imports the document into my currently selected DevonThink group. This has been WAY faster than folder actions which can take many seconds to a minute to activate, where the daemon is almost always nearly instantaneous (as long as there aren’t too many documents in the scan folder.).
Now to the problems: If the scanned file is written to the mac folder fairly quickly, the file imports into DevonThink beautifully. But if the file is written slowly (because, for example, a multipage document is being scanned to a single file) then it seems that the script is being triggered before the scan is finished and before the file is finished being written to the folder. The file does finish writing and (usually) a comment to signify that that file has been imported is written to the comments of the file (The script is set up to only import files without this comment.) BUT, again, no import takes place when a longer document is scanned. (Note: Import does work well on files written more quickly, such as one-page PDF scans, and even on multi-page scans…as long as each page is written to a separate file).
There were two versions of the script. Only the first part being different. The first version included my original idea of reading the size of the import folder every few seconds and then trigger the script when the size stops changing. (That code is at the very bottom of this post.)
The second version, posted below, includes Macscripter forum user adayzdone’s idea for triggering the script properly. Frankly, though, I don’t quite understand how his code exactly functions. However, this second version does, in practice, seem to get marginally better results (fewer documents being skipped/dropped for import into DT.)
The main goal here is to get the scanned items to import successfully no matter how long the scan takes. When this is all worked out I’ll post the finished product here and at MacScripter where I started this thread. It really is amazing how quick using a daemon is compared to folder actions. I think this could help a lot of DT users with a very quick work flow for importing a document into any selected DT group.
Can someone account for the odd behavior and maybe offer a more elegant solution for reliably delaying the import to DT until the longer scans are fully completed and the file is finished being written?
I’m thankful to adayzdone for the suggested code and to MacTipper for the, uh, mac tip.
One other bit of information. Here is the command line Lingon uses to launch the script when the folder is modified:
osascript “/Users/me/Library/Scripts/Folder Action Scripts/Import.scpt”
Here is the script: (I adjusted some of the paths and filenames for security.)
property folder_path : POSIX path of (path to desktop) & "Filers/Scan Folder"
on run
-- This gets the name of the most recent file in the folder. (from adadyzdone)
set xxx to first paragraph of (do shell script "ls -t " & quoted form of folder_path)
-- This checks if the file is busy
tell application "System Events"
repeat until busy status of alias (folder_path & "/" & xxx as text) is false
delay 1
end repeat
end tell
--original script, with slight modifications
tell application "Finder"
set action_folder to ((POSIX file folder_path) as alias)
set folder_items to every item in action_folder
end tell
repeat with an_item in folder_items
tell application "Finder"
set item_name to name of (an_item as alias)
set the_comment to (get comment of (an_item as alias))
end tell
if the_comment does not contain "processed" then
set an_item to (an_item as alias)
tell application id "com.devon-technologies.thinkpro2" to launch
tell application id "com.devon-technologies.thinkpro2"
set theDatabase to "/Users/me/Documents/Data.dtbase2"
set theGroup to current group
try
set thePath to an_item as text
if thePath does not end with ".download:" then
import thePath to theGroup
end if
end try
end tell
tell application "Finder" to set comment of an_item to (the_comment & " processed")
end if
end repeat
end run
Notes on this version including adayzdone’s suggested code:
I scan a long document. The file is written to the scan folder but no import takes place AND no comment is added to the file’s comments section. It appears the script is not triggering properly. THEN, I scan another document. Now, the PREVIOUS document IS imported to Devonthink and the comment “processed” is added to the comments section of THAT file; BUT, the file just scanned is ignored by the script. SO, the script DID trigger as the file was written to the scan folder, but it saw the previously scanned file that didn’t have the comment added, and imported it. (Note: Quick scans (e.g. one-page documents), even back-to-back quick scans in the Scansnap feeder, are imported properly…usually.)
Below is the script code for my original idea of delaying the import:
--My addition to the original script. Here's where I attempt to watch the folder for size changes until the scanned file is finished being written to the folder. (Pressing RUN in the script editor seems to recognize these lines. But not so when the daemon triggers the script.)
repeat
set base_folder_size to size of (info for folder_path)
delay 5
set change_folder_size to size of (info for folder_path)
if base_folder_size is change_folder_size then exit repeat
end repeat