How do I use a Progress Bar?
Learn how to use a progress bar while importing or exporting a file. Sample code is provided.
Progress bars are often used to provide a graphical indication of an action measured against time for an ongoing process, such as when transferring files. Here we demonstrate how to implement a progress bar that can be used when importing and exporting a file.
Create a new main stack with two buttons. One button should be labeled Import, the other button should be labeled Export. Then add the progress bar. In a working application, it may be more appropriate to add the file selection dialog, rather than the Import and Export buttons.
The properties of the progress bar are widely configurable. By default the range is set from 0 to 100, with the current value set to 50. For the purpose of this lesson, set the current value to 0. This allows the progress bar to be updated with percentiles.
Note: A range from 0 to 100 is convenient if the percentage values are to be displayed in numerical terms next to the progress bar.
Much of the code in this example is used to determine the size of the file. The repeat loop then reads in the file in 1024 character chunks until the end of file (eof) marker is reached. As the file is read, a counter is incremented to keep track of the amount of data transferred. That value is then used to update the current value of the progress bar.
on mouseUp global gFileContents # the location of the source file # update this line as required local tImportFileName = "C:/Users/DaveC/Documents/import_me.txt" put empty into gFileContents set the thumbPosition of scrollbar "Progress Scrollbar" to 0 # this section of code is used to find the file size and track # the progress while reading in the file set the itemDel to slash put item 1 to -2 of tImportFileName into tPath put item -1 of tImportFileName into tFileName set the directory to tPath put the detailed files into tFiles # the file name is URL-encoded when using the detailed files # we have to match the encoding here filter tFiles with URLEncode(tFileName) & "*" set the itemDel to comma put item 2 of line 1 of tFiles into tFileSize open file tImportFileName for read put empty into tReadResult put 0 into tAmountRead repeat while tReadResult is not "eof" read from file tImportFileName for 1024 # read 1k characters put the result into tReadResult put it after gFileContents add length(it) to tAmountRead # the following line updates the current position of the progress bar set the thumbPosition of scrollbar "Progress Scrollbar" to (tAmountRead/tFileSize)*100 end repeat # if the file uses LF + CR to mark EOL it will be converted to just one of these # characters here # this means that tAmountRead is slightly smaller than tFileSize # compensate for this mismatch by setting the progress bar to 100 set the thumbPosition of scrollbar "Progress Scrollbar" to 100 close file tImportFileName end mouseUp
As the file is already in memory, it is not necessary to calculate the download size. The export process writes to the file line by line from the internal memory buffer.
on mouseUp global gFileContents # the location of the target file # update this line as required local tExportFileName = "C:/Users/DaveC/Documents/export_me.txt" open file tExportFileName for write set the thumbPosition of scrollbar "Progress Scrollbar" to 0 put number of lines of gFileContents into tNumLines put 0 into tLineCounter repeat for each line tLine in gFileContents add 1 to tLineCounter write tLine & return to file tExportFileName # the following line updates the current position of the progress bar set the thumbPosition of scrollbar "Progress Scrollbar" to (tLineCounter/tNumLines)*100 end repeat close file tExportFileName end mouseUp
The progress bar should be working now. Use the Import button to load a file into memory and use the Export button to write the file back out to the file system.
If you get an error something like
button "Import": execution error at line 30 (Operators /: divide by zero), char 1
check that you changed the filepath to the file you are importing to something valid on your computer. You can't import Dave's actual file :).