Progress Dialog Example
Download Example Project (36K)
This
example shows how to create a progress dialog for a long-running AppleScript
task.
The example demonstrates how to use a progress indicator and how to update
a progress dialog as a task proceeds. It also illustrates how to interrupt
a long-running task.
The Progress Window
Let's start by creating a progress window. In this example, we have a progress
indicator to show how far the task has progressed, two text fields
to describe what's happening, and a button to start and stop the task.
The Code
The code that performs the task is attached to the "Start" button. The progress
dialog has two states: idle and running. When the dialog is idle, pressing
the Start button begins running the task. When the progress dialog is running,
pressing the button sets a flag indicating that the task has been interrupted.
Here's the code:
property gStopped : false
property gRunning : false
property gNumberOfSteps : 5
on clicked theObject
if gRunning then
-- A task is running. Set a flag so that we can
see the user
-- has asked us to stop
set gStopped to true
else
set gStopped to false
set gRunning to true
try
-- Prepare the progress dialog
set title of theObject to "Stop"
tell window of theObject
set string value of text field "msg1" to "Doing
something useful..."
tell progress indicator "progress"
set minimum value
to 0.0
set maximum value
to gNumberOfSteps
set contents to
0.0
set indeterminate
to false
start
end tell
end tell
-- Actually do the work...
repeat with i from 1 to gNumberOfSteps
-- See if the user has interrupted
the task
if gStopped then ¬
exit repeat
set contents of progress indicator "progress" of
window of theObject to i
set string value of text
field "msg2" of window of theObject to ¬
"Step " & i & "..."
delay 1.0
end repeat
on error errMsg
-- Some sort of error happened.
display alert "Error" ¬
attached to
window of theObject ¬
message "Error: " & errMsg ¬
as critical
end try
-- All done, clean up
set title of theObject to "Start"
tell window of theObject
set string value of text field "msg1" to ""
set string value of text field "msg2" to ""
stop progress indicator "progress"
-- Workaround a bug where the AppleScript
Studio's stop progress indicator
-- command does not always redraw the
view
set visible of progress indicator "progress" to
true
end tell
set gRunning to false
end if
end clicked
|