canceling a loop

I’ve searched the forums and it seems that a lot of people have this problem. There are apparently some old posts that answer this question, but they’re out of date and I can’t find them. I feel like this probably has some obvious solution that I’m overlooking.

So here it is…

I have an appllication that is basically a control panel for a bunch of scripts. A button is attached to a particular script. Press a button and that script runs. One of these scripts needs a bit of user interaction, at one point it needs to be told when to stop what it’s doing and continue on with other processes. I’ve created a panel with a progress spinner and a ‘finished’ button on it. Since the ‘on clicked’ handler was used to start the script, I’ve created another script and attached the ‘finished’ button to its ‘on clicked’ handler. But I can’t figure out how to get the main script to recognize that the panel has closed.

A little help? For me and to update the board…

Is the panel a sheet (attached to window “blah”)?

If it is then connect (using the applescript info panel in Interface Builder) your main window to “on panel ended”, and use this code:

on clicked theObject
	set object_name to name of theObject
	if object_name is "open" then
		-- perform scripts
		display window "panel" attached to window "main"
	else if object_name is "close" then
		close panel window "panel"
		display dialog "Sheet closed."
	end if
end clicked

on panel ended theObject with result withResult
	display dialog "Sheet closing."
end panel ended

If it isn’t a sheet try doing this:

on clicked theObject
	set object_name to name of theObject
	if object_name is "open" then
		-- perform scripts
		display window "panel"
	else if object_name is "close" then
		close panel window "panel"
		display dialog "Panel closed."
	end if
end clicked

The panel is a sheet. This doesn’t work because once you’ve clicked the ‘open’ button the script is in a loop and no longer gets any ‘on clicked’ events.

on clicked theObject
	set finishedFLAG to false
	set object_name to name of theObject
	if object_name is "open" then
		display window "panel" attached to window "main"
		repeat while not finishedFLAG
			-- do stuff
		end repeat
	else if object_name is "close" then
		close panel window "panel"
		display dialog "Sheet closed."
	end if
end clicked

on panel ended theObject with result withResult
	set finishedFLAG to true
	display dialog "Sheet closing."
end panel ended

Okay, I see what you mean. I would advise on putting the repeat loop in ‘on idle’. Hopefully that should work.

on idle
	repeat while not finishedFLAG
	end repeat
end idle

I don’t think that will work either since there’s no idle period. It’s an attached panel so it doesn’t stop the execution of the script. The script does many things, the process that needs to be manually interupted is only the first.

Hi,

I’m not exactly sure what you’re doing, but what if you declare finishedFLAG as global or a property.

global finishedFLAG

on clicked theObject
set finishedFLAG to false
set object_name to name of theObject
if object_name is “open” then
display window “panel” attached to window “main”
repeat while not finishedFLAG
– do stuff
end repeat
else if object_name is “close” then
close panel window “panel”
display dialog “Sheet closed.”
end if
end clicked

on panel ended theObject with result withResult
set finishedFLAG to true
display dialog “Sheet closing.”
end panel ended

gl,

The problem is that once you’ve clicked a button to start the script running the ‘on clicked’ handler no longer recocgnizes the ‘stop’ button press.

Tried that a while ago. Didn’t work. I’m not sure why.

Here’s the workaround I’m trying to figure out. I have another script with a separate ‘on clicked’ handler that is called when the ‘finished’ button is pressed. But I can’t figure out how to pass a value between that script and the one that’s running the process that needs to be manually cancelled… Any help on that?

Hi,

Infinite repeat loops often cause problems especially when the script locked in the repeat loop gets user input to exit the repeat loop. One solution for adding a break in the loop is to add the unix sleep e.g. the statement

do shell script “sleep 1”

is placed somewhere in the repeat loop allowing for processing other than the repeat loop. Another way is to use the idle handler. See documentation for more info.

gl,