find last file in list?

n00b here.

i have a script converting video files. once done, it copies the files to a new location.
problem is getting the script to pause while the copying finishes. so far i’m trying to
compare folder sizes by the number of mov files. that works basically but when i get
to the last file being copied i need to use something like this to test whether the copy
is finished:

try
	open for access file x
	--> file is not busy (ie, it's not locked and you can write data to it)
	close access result
on error
	--> file is busy
end try

i realize this is generic at the moment. that’s because i’m stuck trying to figure out
how to get the name of the last file being copied to use it in the example above.

how do i find the last file being copied?

fwiw, here’s the script for the copy part:

tell application "Finder"
	set movCount to count of (files in folder thefolder whose name extension is "mov")
end tell

repeat until movCount is equal to mtsCount
	tell application "Finder"
		set movCount to count of (files in folder thefolder whose name extension is "mov")
		delay 5
	end tell
end repeat

mtsCount refers to the original files being converted. i compare the number of those
files with the number of mov files. when the numbers match, i need to make sure the
last file being copied is completely finished before i move on in the script.

thanks,
BabaG

Well I understand that the copy command don’t wait till the copy is done (your code continues). Well I use ditto command to copy files and don’t have such problem.

An other approach is to check with MD5 between those files. If the MD5 of the source file matches with the target file the copy is completed and you can continue your code. Also a good way to check if every bit is copied.

Also your check isn’t very useful with open for access only. You should use with write permission as well. When I ask the system for a file I give it a flag (read, write). So when a file is opened by a process I still can open it in another process with a read flag but the systems allows only one process with a write flag. So what you should do to check if a file is not opened else were with a write flag is trying to open the file with a write flag yourself. If you can open the file with a write flag you know the file can’t be modified by another process like a copy process for example. So if you do this check after a copy you’re sure that the copy process has closed the file. But like I said open for access can always open the file because the file will be opened with a read flag, so change it in open for access theFile with write permission to change it to open with write flag.

thanks dj.

another question: how do i do this so that the script loops, continuing to test
a file that does not return that it is complete and can be opened for write access?
here’s some code i’m trying at the moment:


tell application "Finder" to (get every file of folder thefolder whose name extension is "mov") as alias list

repeat with AnItem in the result
	try
		open for access file AnItem with write permission
		--> file is not busy (ie, it's not locked and you can write data to it)
		close access result
	on error
		--> file is busy
	end try
end repeat


i’m thinking that this will make a list of all of the mov files, then test each for
write access. it looks to me like it will just return an error when it finds the last
file is not ready yet. how do i loop the routine at that point and tell the loop to
continue until there is no longer an error?

thanks again,
BabaG