Script "wait" until file finishes copying?

Greetings!

I have an applescript that launches when an item is added to a folder, and I’m trying to get it to wait until the file has finished saving to that folder. The script currently starts as soon as the copy starts.

I’ve found a couple posts regarding this, but none of them have worked. This is what I have so far but I believe it’s crashing due to errors. Any insight would be greatly appreciated!



on adding folder items to this_folder after receiving added_items
	tell application "Finder"
		repeat
			try
				set file_size_a to the size of file added_items
				delay 1
				set file_size_b to the size of file added_items
				if file_size_a = file_size_b then
					display dialog "Done copying"
					exit repeat
				else
					-- Still copying...
					delay 3
				end if
			end try
		end repeat
	end tell
end adding folder items to

Many thanks in advance,
Dave

Several things:

  1. I presume that the file being added is from another volume or it wouldn’t be copied to the droplet.

  2. added_items (which you can call anything) is a list. You have to extract the file reference from that list.

  3. getting the size from the Finder is slow - it’s doing the copying. Use set tSize to size of (info for tFile) with your variables.

Adam,

Yes it’s being copied from an external source, not the same drive. Thanks for giving me some direction. I’ll take a look at lists and tSize.

Dave

Success! The script below pauses the applescript until the file(s) that triggered the “on adding folder items” have finished transferring. Thanks for pointing me in the right direction!

on adding folder items to this_folder after receiving added_files
	set x to 1
	set list_length to the length of added_files
	repeat
		set this_file to item x of added_files
		repeat
			set tSize_a to size of (info for this_file)
			delay 2
			set tSize_b to size of (info for this_file)
			if tSize_a = tSize_b then
				display dialog "file " & x & " complete."
				exit repeat
			end if
		end repeat
		if list_length = x then
			exit repeat
		else
			set x to x + 1
		end if
	end repeat
	display dialog "Transfer complete."
end adding folder items to

I know I’m late to the party but this is what I am using. I am not creating a list of files, just checking the size of the folder that is receiving those files until it stops changing.

		--> Wait for client files to finish uploading
		
		repeat
			set x to (info for gCustomerFolder with size)
			delay 1
			set y to (info for gCustomerFolder with size)
			if x = y then exit repeat
		end repeat

As usual. :wink: I’ll be using your version. Thanks!

One question… why is this “wait workaround” even necessary? wouldn’t one think that “on adding folder items to this_folder after receiving added_files” would actually wait until after it actually receives the added files before executing the script?¿

This Seems to work so far on files that are being copied (not folders, at the moment)


on adding folder items to this_folder after receiving added_files
	my check_busy(this_folder)
	--YOU SCRIPT HERE
	
	display dialog "Transfer complete."
	
end adding folder items to

on check_busy(this_folder)
	set busy_ to true
	tell application "Finder"
		
		repeat until busy_ is false
			
			set the busy_items to every file of the entire contents of this_folder ¬
				whose file type contains "brok"
			update items of this_folder
			if busy_items is {} then
				
				set busy_ to true
				exit repeat
			end if
		end repeat
		
	end tell
end check_busy

Another strategy may be to look for the Finder’s copy window to close


tell application "Finder"
    repeat until  "copy" is not in name of the windows
        tell me to do shell script "sleep 1"
    end repeat
end tell

-- continued actions