le’ts say I want to repeat until file is halway done…
repeat until (theFileSize) is greater than or equal to xxxx
end repeat
this is part of a downloader
let’s say my file is 1000 byte, when I download it will start out as 0 byte. I want to repeat until the file has grown to 500 byte (the xxxx)
what should I replace the xxxx with to accomplish this?
set the destfile to ((path to desktop) & "AS.rtf") as text
repeat
set destfilesize to size of (info for (destfile as alias))
if destfilesize > 500 then
exit repeat
end if
delay 3
end repeat
display dialog "File size already > 500 bytes!"
But there is the danger that the file size does not increase (e.g. aborted download), so you should implement a rescue exit (like “repeat 25 times”).
Just to note that ‘info for’ is deprecated (as from Leopard, I think) and will probably stop working at some point in the future. The StandardAdditions dictionary recommends using System Events’s ‘properties of’ facility instead. Since ‘info for’ and other deprecated StandardAdditions commands are much faster than the official alternatives, I’m personally using ‘try’ blocks that will only employ the alternatives when the originals cease to work.
try
-- Deprecated StandardAdditions command.
on error
-- Official alternative.
end try
for anyone it may help, here is working File Download Script with output that can connect to progress bar (here using dialogs)
do shell script "/usr/bin/curl " & "urladress" & " -o " & quoted form of (POSIX path of destination_file) & " -m 9000 > /dev/null 2>&1 & echo $!"
set newSize to 0
delay 2
do shell script "curl " & fileURL & " -I | grep " & "Content-Length: " & " | grep -v grep"
set returnResult to the result as text
set theFileSize to word 3 of returnResult as number
repeat while newSize is not equal to theFileSize
delay 3
set newSize to size of (info for (destination_file as alias))
if newSize < (theFileSize / 3) then
display dialog "Less then 30% completed"
else if newSize < (theFileSize / 1.5) then
display dialog "Less then 60% completed"
else
display dialog "Less then 100% completed"
end if
end repeat
display dialog "success"
how would I go about to use System Events instead?