on idle loop on AS?

Hello everyone! After making some applications on applescript i “upgraded” to Applescript Studio.
But i have a big problem. I want a program to start running after i push a button, and after that i want it to loop the effect, but when i add the “on idle” handler(?) it will start the “on idle” section right away.

Any thoughts?

I’d also like to make a request. How do i make a countdown timer appear in the main window, after the button is pressed and make a countdown of X seconds?

Thank you

This may not be the best advice but I have used this method to make a similar situation work. The “On Idle” handler goes to work right away because before you press the button the app is “idle”. What i did to get around this was to use a global variable and set it to true once the button click is completed. I set the global variable to false in the “on launched” or “on awake from nib” handlers to false so that the function doesn’t start before the button is clicked.

global clickedButton

on clicked theobject
set clickedButton to true
end on clicked

on idle
if clickedbutton is true then
do whatever
end if
end idle

this worked to an extent. Now i get the error “The variable clickedButton is not defined. (-2753)” :S

did you set it to false in a “On Launched” or “awake from nib handler”?

I should also note that to setup the “On Launched” hander go to exactly the same place as you did to setup the “On Idle” handler and check “On launched” as well.

something like this:

on awake from nib theObject
	set clickedButton to false
end awake from nib

?

I am not sure where the problem is because this exact snippet works perfectly for me. I have one button on the main window and a “Awake form Nib” handler attached to the main window


global clickedButton

on awake from nib theObject
	set clickedButton to false
end awake from nib

on idle theObject
	if clickedButton is true then
		display dialog "True"
	end if
end idle

on clicked theObject
	set clickedButton to true
end clicked

now its working :smiley: thank you very much!

Just another approach. I have this in one of my apps to pause the on idle loop when I open the preferences window. You could change it so it loops through looking to see if a button is on or off. That way it wouldn’t go until the button is clicked on. As an added bonus, you could click the button off again at any time and stop the idle loop again:

on idle theObject
	--If preferences window is open, loop until it closes to "pause" the tool.
	repeat while visible of window "prefsWindow" is true --You would use something like repeat while button whatever is off
		tell window "mainWindow" to set contents of text field "statusText" to "Paused while updating preferences..."
		delay 1
	end repeat
--the rest of your script
return 5
end idle

Browser: Safari 419.3
Operating System: Mac OS X (10.5)

I think ill stay with Dallasbr way, but thanks anyway.
I’m having another slight problem though. I added a second button to stop the script, thinking i could use the “set clickedButton to false” to stop the script. However it isn’t working, since i already have a button assigned to that script. On a different script it also doesn’t work :frowning:
Any ideas?

Hi,

“set clickedButton to false” will not do anything because you are just declaring the state of the button. You need to attach an action to that button.
Assuming that the name of your button is “Quit” try the handler below:


on clicked theObject
   set objectName to the name of theObject as Unicode text
   if objecName is "Quit" then
      tell me to quit
      continue quit
   end if
end on clicked

Make sure to connect your button (named “Quit”) to your script before compiling.
Good luck.

archseed :slight_smile:

i thought since the idle script only started if the “set clickedButton” was true, it would stop the loop :S
I tried to add a new button to the (main) script, but since i already had the start button i couldnt compile.
“The «event coVScliI» handler is specified more than once. (-2752)”
My goal is only to stop the loop, not to quit the program (that is what i understand your script does)

Hi keytachi,

Sorry. I read it that you “added second button to stop the script” which I interpreted as quitting from the script, not the loop.
OK, let me get back to you later on for possible solution.

archseed

Hi Keytachi,

I tested the codes below and I know it works with two contrasting buttons in one window controlling actions within the idler handler.

The trick is to reset the boolean variables that control the activation of the two buttons (activateLoop and cancelLoop) within the idler hander. If you don’t reset, then the other button will not work and vice versa.

Below are the codes:


global activateLoop, cancelLoop

on awake from nib theObject
	set activateLoop to false
	set cancelLoop to false
end awake from nib

on idle theObject
	if activateLoop is true then display dialog "Let's get going"
	if cancelLoop is true then display dialog "Let's stop now"
	set cancelLoop to false
	set activateLoop to false
end idle

on clicked theObject
	if name of theObject is "activateLoop" then set activateLoop to true
	if name of theObject is "cancelLoop" then set cancelLoop to true
end clicked

I hope this answers your question.

Good luck!

archseed :slight_smile:

it doesn’t seem to be working for me (i probably messed up somewhere).
I created a new applescript file to test this. I assigned both buttons to this script and pasted the code you so kindly came up with.
However i got this error when building: “4: Expected “end” but found unknown token. (-2741)”
line 4 is this: “set activateLoop to false”

Thank you so much for helping out someone that knows almost nothing about this :slight_smile: I’m learning as i go (mainly through this site), so I probably don’t even know some basic stuff that could be what is causing this errors

Hi,

Some strange item must have come into your code - either some word is missing or something that Xcode does not accept was added. It is expected an “end” somewhere but you have something else there. Sorry, I cannot be of much help finding it unless you show your codes here. If you are not keen on doing that, you can send your code to my email address. I suggest that you check your codes carefully because I know that the one I posted works

Good luck.

archseed :slight_smile:

the thing is, this error appears even when i make a new script and just paste the code you gave me in there :S (maybe that is the problem).
But i will send you the code i have anyway. Thank you for the help!

HI,

OK, please do.

I’d be glad to find out where the problem is.

archseed :slight_smile:

I decided to change my script from “On Idle” to “Repeat”

and i found this script here:

on clicked theObject
        set ChosenButton to the name of theObject
        if ChosenButton is in {"Start"} then
                repeat
                        beep
                        delay 2
                end repeat
        end if
        if ChosenButton is in {"Stop"} then
                --here is where I want the repeat loop to stop
        end if
end clicked

(http://bbs.macscripter.net/viewtopic.php?id=23905)

and this is what i came up with:

on clicked theObject
	set ChosenButton to the name of theObject
	if ChosenButton is {"Start"} then
		repeat
			tell window of theObject
				tell application "System Events"
					tell application "TextEdit" to activate
					keystroke "a"
					delay 10
				end tell
			end tell
		end repeat
	end if
	if ChosenButton is {"Stop"} then
		exit repeat
	end if
end clicked

on should quit after last window closed theObject
	return true
end should quit after last window closed

(for testing purposes i’m using TextEdit and “a”)
I assigned 2 buttons to this script (Start and Stop), but i get a -1708 error when i start the application :frowning:
Any ideas?

Hi,

I am not sure what the error that your application generated means.

Just one wild guess at it here - did you enable the “assistive devices” in the System Preferences? The keystroke command in your application using the System Event works only if the “enable assistive devices” is active.

Good luck.

archseed :slight_smile:

yes i have assistive devices, but is this script correct? theoretically should it work? or do i need to make some changes? i selected the both buttons to “object” instead of “global” but it did nothing :S
any ideas?