Lines of script getting skipped??? (script/Lingon/daemon)

I am using an applescript and Lingon application (based on a MacTipper blog entry) to integrate a Fujitsu ScanSnap scanner with DevonThink, an information manager. 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.

Now to the problems: If the scanned document 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) 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 (the script only imports files without this comment) is written to the comments of the file BUT no import takes place when a longer document is scanned. (Note: Again, import does work well on files written more quickly, such as one-page PDF scans).

To the solution I thought would work: I included some, admittedly klunky, applescript lines to get the folder size every few seconds, the idea being that if the folder size was changing, the file was not finished being written. Now, when I click RUN in the script editor the script does seem to check the folder size as predicted. However, when the daemon triggers the script, it seems like those lines are being skipped. Why do I believe that? Because of the checks I put in: I put scripted in display dialogs of the folder size. Those do display when clicking run in the editor but they don’t display when the daemon triggers the script. I’ve double checked to make sure I wasn’t unwittingly using two versions of the script. Can someone account for the odd behavior and maybe offer a more elegant solution for delaying the script from triggering until the scan is fully completed and the file is finished writing?

I’ve included the code below. I only write script for my own use when needed so sorry for my own lack of elegance. Here it is: (I adjusted some of the paths and filenames for security.)

property folder_path : "/Users/me/Desktop/Filers/Scan Folder"

on run    

--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
   
--Here is where unprocessed files (ie. the file/files just scanned) are "loaded up."
   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)
--Here is where the file in the Scan Folder is actually imported to the selected DevonThink group.
           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

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”

Thanks for any ideas on the odd behavior. 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 on the DevonThink forum. It really is amazing how quick using a daemon is compared to folder actions. I’m thankful to MacTipper for the, uh, mac tip.

Update of 6/30/2012:

I included adayzdone’s suggested script (the second one) in the version of the script below. More interesting behavior: 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. (Note: Writing the scan to folder is not part of the script. That’s managed by the scanning software.) 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.

It appears the script is triggering but is not seeing the scanned file at the proper time, even including adayzdone’s suggestion. Suggestions as to how to get the script/daemon to see the completely written file at the proper time???

Thanks!

Here’s the script with adayzdone’s suggestion:

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 HERE...
	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
--...TO HERE, is the only changed part.
	
	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

I am curious why the folder action would not be right for this task?

on adding folder items to theFolder after receiving theFiles
		repeat with aFile in theFiles
		-- Do things	
		end repeat
end adding folder items to

You can try something like this:


property folder_path : POSIX path of (path to desktop) & "Filers/Scan Folder"

-- This gets the name of the most recent file in the folder
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

@adayzdone

Thanks for the scripting suggestions. I will try and post the results. Folder actions, at least on my system, seem very slow compared to the discussed method. The folder action can take 45 seconds to over a minute to trigger. My understanding is that the system isn’t constantly looking at the folders for folder actions (not sure how intermittent it is), but the daemon is pretty constant. In my experience the daemon triggers the script anywhere from instantaneously to within 10 seconds. I’ve never had a folder action, at least related to the discussed purpose, take less than 20-30 seconds. Thanks again for the suggestions.

I updated my original post to reflect the current progress on the issue. The import script is still not working reliably or predicatably, though it does seem to be getting marginally more consistent results (fewer documents being dropped/looked over). Appreciate any additional thoughts. Thanks.

No help with your main problem, but to explain the above: direct user interaction with osascript isn’t allowed, which means it’ll error when told to display dialogs itself. That’s probably why there was no apparent action when you put dialogs in the script. To display dialogs, scripts run by osascript have to tell an application to display them, preferably one that’s already open, visible, and frontmost.

tell application (path to frontmost application as text) to display dialog "Blah"

Just a couple of thoughts from someone who can’t see into your computer:

  1. Is the scan saved directly to “Scan Folder” or is it saved to the folder whose name ends with “.download” and then transferred when it’s complete?
  2. Does changing the option in adayzdone’s shell script from “-t” to “-u” make any useful difference? (ie. sorting by last-access date rather than modification date?)