calcTimeRemaining
April 23, 2008 in Mac OS X by chip
STEP ONE:
You need a progress scrollbar called “ftpProgress” and a text field called “updateFTPstatus”.
STEP TWO:
Here’s the code for your libURL callback that stores the start time as a custom property:
ON libURLProgress tURL, theStatus
SWITCH item 1 of theStatus
CASE "uploading"
-- let's see if we need to initialize the scrollbar
IF the endValueSet of scrollbar "ftpProgress" is not "true" THEN
--yes, upload just started, we need to initialize
lock screen
show scrollbar "ftpProgress"
set the endvalue of scrollbar "ftpProgress" to item 3 of theStatus
-- set a flag so we don't do this again
set the endValueSet of scrollbar "ftpProgress" to true
set the startDLTime of scrollbar "ftpProgress" to the long seconds
END IF
set the thumbpos of scrollbar "ulProgress" to item 2 of theStatus -- update scrollbar
updateFTPstatus calcTimeRemaining(theStatus, tURL)
break
CASE "uploaded"
resetULscrollbar
updateFTPstatus "ready"
break
CASE "error"
updateFTPstatus "uploading error:" && theStatus
answer "Woops! There has been an error! PANIC!!!!"
exit to top
break
DEFAULT
IF char -1 of tURL = "/" THEN
updateFTPstatus theStatus
END IF
END SWITCH
END libURLProgress
STEP THREE:
Here’s the handler that places the text into the status text field:
ON updateFTPstatus tString
put tString into fld "updateFTPstatus"
END updateFTPstatus
STEP FOUR:
And here’s the one that calculates throughput and formats the string:
FUNCTION calcTimeRemaining theStatus, tURL
put the long seconds - the startDLTime of scrollbar "myProgressScrollbar" into tElapsed
put item 2 of theStatus/1000 into tKdownloaded
set the numberformat to "#"
put tKdownloaded / tElapsed into tKBperSec
put (item 3 of theStatus - item 2 of theStatus)/1000 into tKBRemaining
IF tKBperSec > 0 THEN
put tKBRemaining/tKBperSec into tTimeRemaining
IF tTimeRemaining > 60 THEN
put tTimeRemaining div 60 into tMinutes
put tTimeRemaining mod 60 + 1 into tSeconds
put tMinutes && "minutes" && tSeconds && "seconds" into tTimeLeft
ELSE
put tTimeRemaining && "seconds" into tTimeLeft
END IF
ELSE
put "unknown" into tTimeLeft
END IF
put " remaining @" after tTimeLeft
return "uploading" && fileNameFromPathAndFile(tURL) & cr & tTimeLeft && tKBperSec & " KB/sec"
END calcTimeRemaining
STEP FIVE:
And here’s the one that is called when the operation is over to reset the scrollbar:
ON resetScrollbar
lock screen
set the endValueSet of scrollbar ftpProgress to false
set the thumbpos of scrollbar "ftpProgress" to 0
hide scrollbar "ftpProgress"
hide fld "updateFTPstatus
END resetScrollbar