Copy Status

Hey all

I’ve been trying to work on a script that will start to copy a file from one volume to another and as its copying check the file size of the file as its being completed on the new volume and then compare that to the original file size, and then repeat a dialog box so you know how far a long it is. I am copying over some large files (ex 125GB or so) from one volume to another and have a script that does this but is missing some sort of status. I just want to know…is this possible? I’ve started from scratch about 3 times now and am failing…

Thanks.

You’ll need two scripts to get that one working (more help is coming)

EDIT:
The application exists out of two scripts. A notifier script and the main script.
Main.scpt

on open theObjects
	if (count theObjects) is not 1 then display dialog "Only the first item will be processed"
	set itemToCopy to (item 1 of theObjects) as alias
	set destinationFolder to (choose folder with prompt "Choose a destination folder")
	
	copyItemTo(theObjects, destinationFolder)
end open
on run
	-- kind
	set kindToCopy to choose from list {"File", "Folder"} with prompt "What do you wish to copy?"
	set kindToCopy to kindToCopy as string
	-- item path
	if kindToCopy is "File" then
		set itemToCopy to choose file with prompt "Choose the file to copy"
		
	else if kindToCopy is "Folder" then
		set itemToCopy to choose folder with prompt "Choose the folder to copy"
		
	else
		return
	end if
	
	-- destination path
	set destinationFolder to choose folder with prompt "Choose a destination folder"
	
	copyItemTo(itemToCopy, destinationFolder)
end run

on copyItemTo(itemToCopy, destFolder)
	set newPOSIX to createPOSIX(itemToCopy, destFolder)
	ignoring application responses
		try
			tell application "Finder" to duplicate itemToCopy to destFolder with replacing
		on error theMsg
			display dialog "An error occured: " & return & return & theMsg buttons "OK" default button 1 cancel button 1
		end try
	end ignoring
	tell notfierScript() to notifyCopy(itemToCopy, newPOSIX)
end copyItemTo

on notfierScript()
	(path to me as text) & "Contents:Resources:Scripts:Notifier.scpt"
	load script alias result
	return result
end notfierScript

on createPOSIX(aFile, aDestination)
	set myName to name of (info for aFile)
	set myPOSIX to POSIX path of aDestination
	if myPOSIX does not end with "/" then set myPOSIX to (myPOSIX & "/") as string
	set myPOSIX to (myPOSIX & myName) as string
	return myPOSIX
end createPOSIX

Notifier.scpt

on notifyCopy(orFile, newFilePOSIX)
	set newFileAlias to (POSIX file newFilePOSIX) as string
	repeat
		tell application "Finder" to set myExists to (exists alias newFileAlias)
		
		if myExists is true then
			set orSize to (size of (info for orFile))
			set newSize to (size of (info for POSIX file newFilePOSIX))
			if orSize is not newSize then
				display dialog "Copying: " & (newSize / 1000 / 1000) & " MB / " & (orSize / 1000 / 1000) & " MB" buttons {"¢"} giving up after 1
				--delay 1
			else
				display dialog "Copying done" buttons "OK" default button 1 giving up after 5
				return
			end if
		end if
	end repeat
end notifyCopy

A ready to use droplet/application bundle can be found here: http://aghyour.com/verzenden/download.php?file=634ea79f02e250571d710bc68a17fc89

Hope it works,
ief2

PS: Tested with file size of 8 GB
PPS: I just think of it now, but of course it can also be thrown together in one script and even an idle handler is possible. (why do I always think the hard way first?). I’m improving your script right now!

UPDATE
Save this as a stay-open application

global idleFlag
global doneFlag
global itemToCopy
global destinationFolder
global updateFrequency

on open theObjects
	registerFlags()
	set updateFrequency to chooseUpdateFrequency()
	if (count theObjects) is not 1 then display dialog "Only the first item will be processed"
	set itemToCopy to (item 1 of theObjects) as alias
	set destinationFolder to (choose folder with prompt "Choose a destination folder")
	
	copyItemTo(theObjects, destinationFolder)
end open

on run
	registerFlags()
	set updateFrequency to chooseUpdateFrequency()
	-- item
	set itemToCopy to chooseItem()
	if itemToCopy is missing value then return
	
	-- destination path
	set destinationFolder to choose folder with prompt "Choose a destination folder"
	
	copyItemTo(itemToCopy, destinationFolder)
end run

on idle
	if idleFlag is false then return 1
	if doneFlag is true then
		display dialog "Copying Done" buttons "OK" default button 1 cancel button 1
	end if
	
	if idleFlag is true then
		set newFilePOSIX to createPOSIX(itemToCopy, destinationFolder)
		set newFile to (POSIX file newFilePOSIX) as string
		
		tell application "Finder" to set myExists to (exists alias newFile)
		if myExists is true then
			set orSize to (size of (info for itemToCopy))
			set newSize to (size of (info for (alias newFile)))
			if orSize is not newSize then
				display dialog "Copying: " & afrondenNaarDecimalen((newSize / 1000 / 1000) as real, 2) & " MB / " & afrondenNaarDecimalen((orSize / 1000 / 1000) as real, 2) & " MB" buttons {"¢"} giving up after updateFrequency
			else
				set doneFlag to true
			end if
		else
			display dialog "Initializing copy" buttons {"¢"} giving up after 1
		end if
		
		return 0.01
	end if
end idle

(* ===== HANDLERS ===== *)
on copyItemTo(ITC, DF)
	ignoring application responses
		tell application "Finder" to duplicate itemToCopy to destinationFolder -- with replacing
	end ignoring
	set itemToCopy to ITC
	set destinationFolder to DF
	set idleFlag to true
end copyItemTo

on chooseItem()
	-- kind
	set kindToCopy to choose from list {"File", "Folder"} with prompt "What do you wish to copy?"
	set kindToCopy to kindToCopy as string
	-- item path
	if kindToCopy is "File" then
		set itemToCopy to choose file with prompt "Choose the file to copy"
		return itemToCopy
		
	else if kindToCopy is "Folder" then
		set itemToCopy to choose folder with prompt "Choose the folder to copy"
		return itemToCopy
		
	else
		return missing value
	end if
end chooseItem

on chooseUpdateFrequency()
	
	set myFrequency to text returned of (display dialog "Please choose an update frequency" default answer "5")
	try
		myFrequency as integer
	on error
		set myFrequency to chooseUpdateFrequency()
	end try
	if (myFrequency as integer) < 1 then set myFrequency to chooseUpdateFrequency()
	
	return myFrequency
end chooseUpdateFrequency

on registerFlags()
	set idleFlag to false
	set doneFlag to false
end registerFlags

on createPOSIX(aFile, aDestination)
	set myName to name of (info for aFile)
	set myPOSIX to POSIX path of aDestination
	if myPOSIX does not end with "/" then set myPOSIX to (myPOSIX & "/") as string
	set myPOSIX to (myPOSIX & myName) as string
	return myPOSIX
end createPOSIX

on afrondenNaarDecimalen(aFloat, aantalDec)
	try
		if aantalDec is 0 then return aFloat as integer as string
		if aantalDec < 0 then return ""
		set wholeFloat to (aFloat as real) as string
		if wholeFloat contains "," then set AppleScript's text item delimiters to ","
		if wholeFloat contains "." then set AppleScript's text item delimiters to "."
		
		set myInt to text item 1 of wholeFloat
		set myDec to text item 2 of wholeFloat
		
		set AppleScript's text item delimiters to ""
		
		if (count every character of myDec) < aantalDec then
			set zerosToAdd to (aantalDec - (count every character of myDec)) as integer
			set newDec to {myDec}
			repeat zerosToAdd times
				set end of newDec to 0
			end repeat
			set myDec to newDec as string
			
		else
			set myDec to ((characters 1 thru aantalDec) of myDec) as string
		end if
		set newFloat to (myInt & "." & myDec) as string
		
		return newFloat
	on error
		return "(null)"
	end try
end afrondenNaarDecimalen

Hope it works,
ief2

…Damn…hahaha. That is awesome! I wasn’t even expecting a script that rocks! I’ll put it to the test tonight and see how it goes. I had the second script pretty similar to that I was just missing all the other stuff haha. Again…thanks a lot!