Preventing a script from getting ahead of itself...


I have a script that I designed to monitor a server folder for new items. When it sees a new file in the folder, it copies the file to the local HD (to performs some tasks in Photoshop). After the file gets copied to the HD, the script deletes it from the server.

The script is having this arbitrary problem. Sometimes, it seems to try and delete the file from the server before it’s done copying down. I get the Finder message “Can’t move XXX to the trash, because the file is busy…”. It’s hard for me to pinpoint exactly what the problem is, because there’s no noticeable pattern. I can redo the same thing with the same file, and it works.

So, is there a way to prevent the script from trying to delete the file from the server before the file is done copying down? I’ve tried playing with ‘busy status,’ without success.

The gist of my script is simple:

--If the script sees a file in the server folder:
tell app "finder"
    move aFile to localFolder
    delete aFile
end tell



You can use an error handler which allowsthe script to continue only if there is no error. Something like this:

–If the script sees a file in the server folder:
tell application “Finder”
move aFile to localFolder
repeat 1000 times – it had enough
delete aFile
exit repeat
on error
do shell script “sleep 1”
end try
end repeat
end tell

The necessity of the repeat count or the delay here depends on you.