Scripting - Repeat Until

I’m trying to make a program that repeats a group of actions over and over again. I want the program to end either if the program has repeated a certain number of times or if a certain key is pressed (specifically the escape key). Is there an easy way to do this? If so, what? If not, what would the complicated way be?

What I want to be done:

repeat x times or until esc pressed

action

end repeat

What would the coding for this look like in AppleScript?

AppleScript really isn’t set up to detect a key down event.

Is there a way to detect it with “Extra Suites” or something along the lines of it? Because I know that Extra Suites at least made it easier to do mouse movement.

The Extra Suites Dictionary doesn’t list anything.

I posted this question on several different forums and received this:

"You are going to have to do two things for this:

  1. Use an “on keyboard down” handler to fire whenever the escape key is pressed and set a variable to tell the other portion that it should stop.

  2. Use a “on idle” handler to actually do the work. You are going to need to set a variable to count down the repetitions in this, and stop calling this back (ie: don’t return anything, or drop out of the script).

Information on both of these can be found using google searches with “AppleScript” + the name of the handler."

as an answer but I’m not exactly sure what it means. I have very little programming experience and am not sure where I would begin if I decided to follow what this says. Could any information be given to try to help explain this or to comment on whether it would really work or not?

Hi,

Would including something like the script below in your own script be of any help ?

on run
	tell application "KeyboardViewerServer"
		activate
	end tell
end run

on idle
	tell application "System Events"
		if ((get value of checkbox "esc" of first window of application process "KeyboardViewerServer") as number) = 1 then
			tell me to quit
		end if
	end tell
	return 0.05
end idle

like you, i am very new to scripting, but the above does seem to quit the script on clicking the Escape Key.Perhaps you could add the rest of your script to the on idle handler.

Val

I’m really not sure if it would. I’m, actually, really not quite sure what that would do at all. What is “KeyboardViewerServer”? And, what does “on idle” do? (Yes, I know it’d be a very easy question for many to answer, but, as I said, I’m VERY new to this.) Finally, what does that entire if statement (

if ((get value of checkbox "esc" of first window of application process "KeyboardViewerServer") as number) = 1 then
	tell me to quit
end if

) do?

Any help or explanation would be greatly appreciated.

KeyboardViewerServer appears to be the previous Leopard implementation of the Snow Leopard app KeyboardViewer - /System/Library/InputMethods/KeyboardViewer
(i.e. KeyboardViewerServer appears to have been replaced)

So depending on which version of the OS you are using you may or may not have it. (you can check with spotlight)

If you have upgraded to Snow Leopard and not deleted the “Previous System” folder the KeyboardViewerServer app may still be on your machine and useable.

This is relevant as my previous post only related to KeyboardViewerServer (and not to KeyboardViewer. I can’t get it to respond to a similar script).

Again the qualifier that I am an absolute newbie should be kept very much in the foreground.

on idle

When saving a script if you “Save as” Application and check the Stay open box then assuming your script is inside an on idle--------end idle statement it will stay open and execute your script at whatever interval you decide. The ‘return 1’ at the end basically asks the script to repeat every second. It could be ‘return 5’ (ie repeat every 5 seconds) or whatever. There are posts on this site that will give you a far far better explanation.

The If statement

the value of checkbox “esc” of first window of application process “KeyboardViewerServer” changes from 0 to 1 when the Esc key is pressed. The Script is saying if Esc is pressed tell me (the script) to quit.

There is more info on this particular script at http://macscripter.net/viewtopic.php?pid=93883 . The poster Mark Hunte has posted to other scripting fora about this. So my info is coming from this site and others - not one jot of originality.

Also at http://cross-the-sea.blogspot.com/2008/12/is-option-key-pressed.html

If you happen to have KeyboardViewerServer on your machine then to confirm that the script works, use it to open an application say TextEdit (see script). You can then replace this with your own requirements.

on idle
	tell application "System Events"
		if ((get value of checkbox "esc" of first window of application process "KeyboardViewerServer") as number) = 1 then
			tell application "TextEdit" to activate
			tell me to quit
		end if
	end tell
	return 0.05
end idle

Save As application and check “Stay Open”. Then activate it. This will run until you press Escape. It will then open TextEdit and Quit.

Val

Thank you very, very much. That did work. :slight_smile:

Now, I only really have one unresolved issue with my program. Some of the clicks aren’t registering for some reason. I think this reason may be that the program I’m trying to click in isn’t ever the front window. Is there any way to make the program come to the front for each click or for the entire duration of my program running? Something along those lines at least, though I’m relatively sure I phrased that completely wrong.

Try inserting -when required-

activate application "whatever name"

Cheers,

Val

Thanks so much Val.

Is there any way to make sure that an application’s window is in a particular spot on the screen?

Try

set bounds of window “whatever” of application “whatever” to {w,x,y,z"}

as in

tell application "TextEdit"
	activate ----- if not already active or to bring to frontmost
	set bounds of window "untitled" to {150, 1085, 627, 1125} ----replace with your choice
end tell

To obtain the bounds of an existing window named “untitled” of application “TextEdit” (say)

tell application "TextEdit"
	get bounds of window "untitled"
end tell

Check it out anyway.

Val

You’re a life saver Val. :slight_smile: You’ve saved me so much time. I think I’ve got everything now. Thank you very, very much.

Could something like this:

tell application "Extra Suites"
	tell application "Safari"
		activate
		set bounds of window "Top Sites" to {160, 22, 1219, 800}
	end tell
	ES move mouse {510, 273}
	ES click mouse
	delay 1
end tell

be done and have Safari as the front window for the mouse move and click? Or would an extra

tell application "Safari"
	activate
	set bounds of window "Top Sites" to {160, 22, 1219, 800}
end tell

have to be put in like this:

tell application "Extra Suites"
	tell application "Safari"
		activate
		set bounds of window "Top Sites" to {160, 22, 1219, 800}
	end tell
	ES move mouse {510, 273}
	tell application "Safari"
		activate
		set bounds of window "Top Sites" to {160, 22, 1219, 800}
	ES click mouse
	delay 1
end tell

so that Safari stays on top for both actions? (I know it’s not necessary to have the “set bounds of window” in both [or possibly either] of them but it makes sure that they’re in the right spot the entire time which is vital to the successful execution of the program.)

Afraid I don’t have "Extra Suites. Nor would I have the expertise to suggest anything. Perhaps you might post the query under another heading.

Val

Thanks Val. I did just that.