Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Interrupt repeat loops with buttons

Hi,

How do I make buttons available to the user while I run a repeat loop?

I'm doing a shell script in the background and I'm using a repeat loop to read out the log of the results. Meanwhile, I want to have a Cancel button available to the user.

The button however doesn't respond while the repeat loop runs, while the rest of my UI is in fact updating (based on the incoming log). I'm even using a one second delay at the beginning of the loop.

I'm using AppleScript Studio in Xcode/Interface Builder.


Thanks,

JW

MacBook 13.3" 2.0 GHz, Mac OS X (10.4.7), 1 GB RAM

Posted on Jan 11, 2007 2:31 AM

Reply
7 replies

Jan 11, 2007 9:05 AM in response to Joris Witteman

Hi Joris,

Instead of using 'delay', you can use an idle handler which doesn't use the cpu for n seconds. You connect the idle handler to the application object. You can also try using unix 'sleep' command:

do shell script "sleep 1"

although I think the idle handler works better because 'do shell script' opens a shell.

gl,

Jan 11, 2007 10:12 AM in response to Joris Witteman

The cancel button sends a message back to your script informing it that the user clicked cancel. It's up to you to react to that message in whatever way you want - presumably by exiting the loop, but maybe also killing the shell process?

Here's one simplified example:

<pre class=command>repeat
set theDialog to display dialog "Do you want to continue?" buttons {"No", "Yes"} default button 2 giving up after 2
if button returned of theDialog is "No" then exit repeat
end repeat</pre>

Unless your script reacts to the button clicked it doesn't matter which button you press.

Jan 12, 2007 7:59 PM in response to Joris Witteman

1. Create a control in the window which has some sort of boolean property (if you are using a progress bar, then you already have one, since a progress bar can be indeterminate, and you can check this).
2. Set the handler which shows the window (or alters the window when the process begins) to set this boolean property to a particular value
3. Have the "Cancel" button handler invert the property
4. Have the repeat loop check the property as part of its boolean condition (as in "repeat while x is true")

Note that if the window is going to go away anyway when the process is complete, then you can use the window's title to indicate the status: set the title to "Cancelling..." and have the repeat loop check whether the title of the window is "Cancelling...".

You could also just create a global within the script file, and have the repeat loop check the status of the global, but that tends to lead, sooner or later, to overlapping reuse of a single global value, which is a bug.

Jan 17, 2007 3:04 AM in response to Camelot

Actually the button is never succesfully clicked, because the UI elements (that come out of ProjectBuilder) are unresponsive to mouse operations while my repeat loop runs.

I need to know how to run a repeat loop, that already properly updates my progress bar element, and still have the Cancel button (or whatever button) available to user intervention.

Interrupt repeat loops with buttons

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.