Repeat until this is true or that is true

Is it possible to set up such a statement or is there another work around. I spent quite a while trying both the repeat statement and the timeout statement. What i want to happen is a part of the script to repeat until something happens, or give up after 4-5 seconds.

I have a work around using this method but just wondered if there was anything that was actually designed for this operation or a more elegant work around.


set cocount to 0
set theKeyPressed1 to ""
repeat until theKeyPressed1 is not ""
set theKeyPressed1 to (keys pressed) as string
set cocount to cocount + 1
if cocount is equal to 40000 then
return
else

end if

end repeat

iMac G5 17 1.8ghz 1GB RAM AX, Mac OS X (10.4.7)

Posted on Jul 28, 2006 11:33 AM

Reply
7 replies

Jul 28, 2006 11:55 AM in response to Richard Moore

Repeats are pretty much all you got. Instead of 'return' (which could have unintended consequences) you can use the 'exit repeat' command instead. It's legal to combine two conditionals on the 'until' or 'while' parameter. The full range of possibilities are described here.

click here to open this script in your editor <pre style="font-family: 'Monaco', 'Courier New', Courier, monospace; overflow:auto; color: #222; background: #DDD; padding: 0.2em; font-size: 10px; width:400px">set wait_time to 4 -- seconds to wait (roughly)

set {i, loop_count} to {1, wait_time * 10}
repeat until (keys pressed) is not {} or i ≥ loop_count
set i to i + 1
delay 0.1
end repeat

say "Hi."

</pre>

Jul 28, 2006 12:15 PM in response to Richard Moore

The issue with using loop iterators (such as 'set i to i+1' and waiting until it reaches a specific number) is that the time taken will vary depending on the speed of your machine, the load on the system and many other factors.

Adding a delay in the loop doesn't help either since all script processing will pause during the delay, preventing your other code (e.g. the code that checks if something's happened).

A better approach would be something like:

<pre class=command>set expireTime to (get current date) + 5 -- the expiration time
set exitflag to false
repeat until (exitflag is true) or ((current date) > expireTime)
-- your code here
-- just set exitFlag to true when you've got your exit condition
end repeat</pre>

Jul 28, 2006 12:34 PM in response to Richard Moore

Here is one example solution:

set cocount to 0

repeat until ((keys pressed) contains "Escape")
set cocount to cocount + 1
if cocount is equal to 40000 then exit repeat
end repeat

display dialog ("coconut: " & cocount & return & ("Key pressed: " & ((keys pressed) as string))) buttons {"OK"} default button "OK"

-----

The above example requires the installation of ' Jon’s Commands.osax', into the '/Library/ScriptAdditions/' folder. The '.osax' is not Intel based Mac compatible.

Consult the ' Applescript-users' mailing list archives for the Subjects 'Jon's CommandsX 3.0d3', 'Keys Pressed', and 'Keys Pressed OSAX' for limitations and current events of 'Jon's Commands.osax'.

Mac OS X (10.4.4)

Jul 28, 2006 3:50 PM in response to Camelot

The issue with using loop iterators (such as 'set i
to i+1' and waiting until it reaches a specific
number) is that the time taken will vary depending on
the speed of your machine, the load on the system and
many other factors.

Adding a delay in the loop doesn't help either since
all script processing will pause during the delay,
preventing your other code (e.g. the code that checks
if something's happened).



Yeah i realized those two points when trying to work it out myself.


A better approach would be something like:

<pre class=command>set expireTime to (get current
date) + 5 -- the expiration time
set exitflag to false
repeat until (exitflag is true) or ((current date) >
expireTime)
-- your code here
-- just set exitFlag to true when you've got your
r exit condition
end repeat</pre>



Thanks for the code, it works perfectly. I was almost there in a previous attempt but so of the finer points were missing.

The final script that i will be using for this purpose is

set expireTime to (get current date) + 2
set exitflag to false
set theKeyPressed1 to ""
repeat until (exitflag is true) or ((current date) > expireTime)
set theKeyPressed1 to (keys pressed) as string
if theKeyPressed1 is "F5" then
set exitflag to true
end if
end repeat


Thank you all for your suggestions.

Jul 28, 2006 3:58 PM in response to Richard Moore

Yet another way is to use the osax getMilliSec downloaded here: http://osaxen.com/files/getmillisec1.0.1.html. It measures the number of milliseconds since the last system startup.

set T to GetMilliSec
set Elapse to 0
set C to 0
repeat until ((keys pressed) contains "Escape" or (Elapse > 3))
set C to C + 1
set newT to GetMilliSec
set Elapse to (newT - T) / 1000
end repeat

display dialog "Count was " & C & return & "Key pressed was " & ((keys pressed) as string) & return & "Elapsed Time was " & Elapse buttons {"OK"} default button "OK"

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Repeat until this is true or that is true

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