waiting for a temp file to finish copying, error catching

Hello All,
recently, I found a script I had created, keeps failing because the file is never finished copying to the folder before the script ran and found the file wasn’t all there. The major problem in catching this file is, that as soon as the file finishes copying into the folder and it’s data is queued up by the printer, the file is removed by the application that creates it. Does any one have a suggestion on how I can catch the file on finish but before deletion?

Chris

I had a similar problem where I was writing postscript files from Quark, to a folder that was essentially a print queue. To stop the print queue from processing the file before it was fully written, I copied the file to a folder, one level up, then moved it to the watched folder after it was fully written to the remote volume.

Hope this helps,

I am not sure if it does or not. Were the postscript files user created?
The situation here is that the printer monitor (epson’s) creates a temp file as it send the data to the printer. this file only exists to hold the data till the printer is ready to take it. So while, I could, by hand, pause the printer, and then move the file, I just don’t see how I can catch the file with your suggestion as I can’t control the print monitor’s behavior. This would be so easy if the print monitor was scriptable :slight_smile:

Chris

This is likely to be a tough nut to crack given the short time frame that you have to work with. You could probably set up a repeat loop that checked the size of the file and then, when the file size stops changing, duplicate it. I suspect that the script would be prone to failure though, given the short window of opportunity to copy the file.

I wonder what happens when Finder tries to duplicate an incomplete file (at the request of a script). If it returns a useful error, a try block might be used instead of checking the file’s size. Something like this untested code:

set status to false
repeat until status is true
	try
		tell application "Finder" to duplicate file "path:to:file" to folder "path:to:folder:"
		set status to true
	on error
		set status to false
	end try
end repeat

Rob Says:
You could probably set up a repeat loop that checked the size of the file and then, when the file size stops changing, duplicate it. I suspect that the script would be prone to failure though, given the short window of opportunity to copy the file.

I say:
you suspect right. that was the first attempt I went after. I caught about 1 in 10 files.
I suspect that your idea for a “try” might work better if for no other reason than it forces the finder into two over lapping commands (copy and delete) on the same file at the same time. I’ll let you all know.

Chris

In looking at my sample code, I can see where the script might go into an endless loop if a file is never successfully copied. You might need to add another try block, or add some type of limit (number of tries, time limit, etc.) to make sure that the script can get out of the loop.

So Here is the sample script I am working with:

set theFolder to alias “blue:System Folder:PrintMonitor Documents:EPSON Spool Folder6:”
on adding folder items to theFolder
tell application “Finder”
set spoolfiles to name of the first file of folder “Epson Spool Folder6” of folder “PrintMonitor Documents” of folder “System Folder” of disk “blue”
end tell
tell application “Finder”
activate
mount volume “volume” on server “server” in AppleTalk zone “zone-foo” as user name “me” with password “wodinchyawannano”
select file spoolfiles of folder “EPSON Spool Folder6” of folder “PrintMonitor Documents” of folder “System Folder” of startup disk
set status to false
repeat until status is true
try
copy selection to disk “logging”
set status to true
on error
set status to false
end try
end repeat
put away disk “logging”
end tell

end adding folder items to

this returns an error (from I suspect the finder as it’s a pop up window):
An error -5016 occurred while the folder actions server was executing a script.

any thoughts? should I attempt duplicate instead?
Chris

Yes, use duplicate.

My thanks to both Rob and Mytzlscript.

after checking up what the heck error -5016 is and rebooting the final destination (sheesh, no wonder it wasn’t working) the script seems to work pretty well

both copy and duplicate work fine as long as the detination is working.
For those of you who might want or need to capture a temp like like this, Rob’s suggestion of try is far better than file size as the try seems to be ready to go as soon as the file finishes copying to the destination, and more importantly it does so before being sent on to the printer

in otherwords it catches the files before anything else happens to them by being in the finder’s queue first.

Later
Chris

If the folder action script (above) works, this should work without relying on selected files. It’s always best to avoid working with selections when possible since a user can mess things up by selecting something else during the execution of the script.

on adding folder items to theFolder

	tell application "Finder"
		set spoolfile to the first file of theFolder
		mount volume "volume" on server "server" in AppleTalk zone "zone-foo" as user name "me" with password "wodinchyawannano"
		set status to false
		repeat until status is true
			try
				copy spoolfile to disk "logging"
				set status to true
			on error
				set status to false
			end try
		end repeat
		put away disk "logging" 
	end tell
	
end adding folder items to

– Rob