Check whether file it used or not

Hi,
first of all, sorry for my english, I hope you understand my question/problem. First of all, I’m new in scripting, so I’m not very experienced.

I have several difficulties with a folder action script. Perhaps I should explain the setup first. Here in my office I have a network-scanner witch stores files on a network-share. I tried to create a folder action script witch checks, whether there’s a new file and if so starts an oct-application.

The biggest problem is, that the scan-process can last a long time, especially when a larger document is scanned. So I need a check whether the file is used or not. The busy-status from the file info/system events doesn’t work, because the scanner doesn’t set the status.

So I wrote this script, witch worked very well


try 
   set this_item to "homeserver:SanDisk-CruzerBlade-01:scan.pdf" 
   --Abfrageschleife ob Datei benutzt wird, darin OCR-Aufruf 
   repeat 20 times 
      try 
         open for access file this_item -- wenn möglich, Datei nicht in Benutzung 
         close access result 
         ocr(this_item) 
         exit repeat 
      on error -- Datei in Benutzung 
         delay 3 -- Warte insgesamt 1 Minute 20 x 3 
      end try 
   end repeat 
on error errText 
   display dialog "Error: " & errText 
end try 
end adding folder items to 

-- OCR-Funktion 
on ocr(scan_file) 
   tell application "ExactScan Pro" 
      activate 
      set profile to 11 
      open scan_file 
      delay 5 --warte kurz 
      repeat until processing is false 
      end repeat -- wird noch gearbeitet? 
      close scan_file 
      delay 5 --warte kurz 
   end tell 
end ocr 

Then I tried to make a folder action, but it doesn’t work and I’ve no idea why not. The folder action works, when I copy files to the folder and the check-process isn’t in the script, when I put the “open for access…” then it doesn’t work


on adding folder items to this_folder after receiving these_items 
   try 
      repeat with i from 1 to number of items in these_items 
         set this_item to item i of these_items 
         --Abfrageschleife ob Datei benutzt wird, darin OCR-Aufruf 
         repeat 20 times 
            try 
               open for access file this_item -- wenn möglich, Datei nicht in Benutzung 
               close access result 
               ocr(this_item) 
               exit repeat 
            on error -- Datei in Benutzung 
               delay 3 -- Warte insgesamt 1 Minute 20 x 3 
            end try 
         end repeat 
      end repeat 
   on error errText 
      display dialog "Error: " & errText 
   end try 
end adding folder items to 

-- OCR-Funktion 
on ocr(scan_file) 
   tell application "ExactScan Pro" 
      activate 
      set profile to 11 
      open scan_file 
      delay 5 --warte kurz 
      repeat until processing is false 
      end repeat -- wird noch gearbeitet? 
      close scan_file 
      delay 5 --warte kurz 
   end tell 
end ocr 

So, if anybody has an idea it would be great.

Thanks a lot

Stefan

I seem to remember a folder action cannot use subroutines.
Put the subroutine into the repeat loop, in place of it’s call, and see what happens.

:frowning: Thanks for your reply, but it also does´t work… It#s a pity…

PS: That´s what I´ve tried


on adding folder items to this_folder after receiving these_items 
try 
repeat with i from 1 to number of items in these_items 
set this_item to item i of these_items 
--Abfrageschleife ob Datei benutzt wird, darin OCR-Aufruf 
repeat 20 times 
try 
open for access file this_item -- wenn möglich, Datei nicht in Benutzung 
close access result 

tell application "ExactScan Pro" 
activate 
set profile to 11 
open this_item 
delay 5 --warte kurz 
repeat until processing is false 
end repeat -- wird noch gearbeitet? 
close scan_file 
delay 5 --warte kurz 
end tell

exit repeat 
on error -- Datei in Benutzung 
delay 3 -- Warte insgesamt 1 Minute 20 x 3 
end try 
end repeat 
end repeat 
on error errText 
display dialog "Error: " & errText 
end try 
end adding folder items to 

Another thought: the try block with the ‘open’ statement fails silently when there are errors. Modify to report errors:

try
	 open for access file this_item -- wenn möglich, Datei nicht in Benutzung
	close access result
	ocr(this_item)
	exit repeat
on error errStr number errNum -- Datei in Benutzung 
	display dialog "Error: " & (errNum as string) & return & errStr
	delay 3 -- Warte insgesamt 1 Minute 20 x 3 
end try

For good measure, add error trapping to the OCR code:

try
	--
	-- OCR code
	--
on error errStr number errNum 
	error "OCR-error: "& errStr number errNum
end try

Now, if OCR has an error, it will be passed back to the script, and reported there.

Hi alastor933,

thanks for your reply, I´ve added your code and now I have an error-message!

Error: -1700 file (alias “Macintosh HD:Users:info:Desktop:test:example.gif”) kann night in Typ <> umgewandelt werden.


How are you getting this_item? I’m almost certain the error originates in the open command. If this_item is an AppleScript alias you’d expect this error. Remove ‘file’ and see how it goes. Consult the Standard Additions dictionary to find out which command takes which kind of object.

:D:D:D:D

You are my hero! Just such a little word like file, it’s amazing! It works perfect! Thanks a lot!