folder actions - how to wait until file has been copied

Hello all, Im new to Applescript programming but not to programming :slight_smile:

I want to trigger an action (movie conversion) after a file has been added to the folder

the folder in question is shared, the files will be copied into the folder via SMB from a windows machine. the files are large and the copy process takes a while

my question is: how to trigger the folder action to start only when the file(s) copy process has been finished? using the statement

on adding folder items to this_folder after receiving added_items
...
end adding folder items to

the folder action begins the moment a new file is beeing copied into the folder, that is a long time before the file is actually available in full

thanks for any insight
-captnswing

My first post on here so Hi all.
I am real interested in this problem as I am running an FTP server with a folder action script to send an email to the production team when a file conmes in and includes a link back to the file.

Only problem is that the email is generated as soon as the file starts to be transfered to the server and people can then try to remove the only partialy transfered file.

Any help on this would be really aprechiated.

Thanks
James Turner
Mac Factory

The ‘info for’ command in the standard additions has a busy status flag that you can check in a repeat loop. I have found that this isn’t always accurate though. There is also a scripting addition called ‘jon’s commands’ that has a command ‘isBusy’ that is usually more reliable. You could probably put them both in a repeat loop so if one or the other says it’s busy then you just repeat the check before going on to your next command.

HTH -john

Here is a Folder Action to look for new files added and WAIT for them to finish writing before processing the added file(s). You also will need my isDone() handler from Code Exchange here on MacScripter.


global CurrentFileSize, LastFileSize, StillBusy, StillGrowing
on adding folder items to this_folder after receiving added_items
	try
		-- get the name of the folder
		tell application "Finder"
			set the folder_name to the name of this_folder
			-- find out how many new items have been placed in the folder
			set the item_count to the number of items in the added_items
		end tell
		ProcessAddedItems(this_folder, added_items, item_count)
	end try
end adding folder items to

on ProcessAddedItems(this_folder, added_items, item_count)
	-- loop through the files looking for finished files to process
	repeat with x from item_count to 1 by -1
		set File2Check to (item x of added_items) as string
		set AppleScript's text item delimiters to ":"
		set TheFileName to text item -1 of File2Check
		set AppleScript's text item delimiters to ""
		set LastFileSize to -1
		set Ready2Rock to false
		beep
		
		--loop waiting for the file to finish writing
		repeat until Ready2Rock is true
			-- modify the path accordingly - point to isDone().scpt on your machine 
			set testResults to run script alias "path:to:isDone().scpt" with parameters {File2Check, LastFileSize}
			set CurrentFileSize to item 2 of testResults
			if item 1 of testResults is true then
				set Ready2Rock to true
			else
				set LastFileSize to CurrentFileSize
			end if
			delay 1
		end repeat
		
		-- now that the file has finished writing,
		-- process the file while it's in this folder 
		-- because the file is so big
		tell me
			activate
			display dialog ("The file: " & return & return & TheFileName & return & CurrentFileSize & return & return & " is ready to process.") ¬
				buttons {"OK", "Cancel"} default button 1 giving up after 2 without static
		end tell
		-- (YOU MAY OR MAY NOT WANT TO DO THE FOLLOWING, DEPENDING ON YOUR NEEDS)
		-- then move the file to a "done" folder
		-- so the folder action can pick up
		-- a new file with the same name 
		-- because Folder Actions don't "see" overwrites
		-- only new files added are acted upon, not updates or overwrites
		--            tell application "Finder" to move alias (file_to_check) to folder "path:to:done:folder:" -- intertpret as necessary
		
	end repeat
end ProcessAddedItems

Jeff Case

John, Jeff, everybody - THANK YOU!