Curl, Applescript and Progress Bar
Curl, Applescript and Progress Bar
- Subject: Curl, Applescript and Progress Bar
- From: Phong Le <email@hidden>
- Date: Wed, 2 Feb 2005 19:35:39 -0800
After a hours of searching around for bits of answers, I was able to build a download/upload system in applescript that has a progress bar. Here's how to do it.
You basically need to do four things
1. Get the actual size of the file to be downloaded
2. Start the download as a background process
3. Repeatedly check the size of the download until it matched the actual size
3. Feed the information into a progress bar
To get the actual of the file on the server, I use curl and pipe the output to grep
do shell script "curl " & fileURL & " -I | grep " & "Content-Length: " & " | grep -v grep"
set returnResult to the result as text
set thefilesize to word 2 of returnResult as number
It is important to start the download as a background process in order to run the loop for the progress bar. It's not necessary to save the pid below but it is useful if you want to abort the download
do shell script "curl " & fileURL & " -o " & output_path & " -m 9000 > /dev/null 2>&1 & echo $!"
set pid to the result
Set up a loop to monitor the size of the downloaded file. I find that applescript's size of file does not give the correct size and often hangs. Instead I go into the shell and pipe the ls command to grep and finally split the result. It's pretty ugly as you can see, but it works well. If anyone knows of a better method, please post.
set to 0
repeat until equals thefilesize
tell application "Finder"
do shell script "ls -la " & download_folder & " | grep " & downloadfilename & " | grep -v grep" set ls_result to the result
set AppleScript's text item delimiters to space
set thearray to ls_result's text items
set thecount to count (thearray)
set downloadedSize to item thecount - 5) of thearray
end tell
--feed the downloadedSize and the actual file size into any number of progress bar.
end repeat
There are many progress bars out there for you to use.
I use the free ProgBar from www.sporksoftware.com They all basically work on the same concept: feed it the current size of the download and the actual size of the download. Remember, you feed the progress bar while inside the loop.
I hope this helps someone. Help or question: email me at email@hidden
Phong Le
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden