Repeat Loops
One of the great strengths of computers is their ability to perform repetitive tasks with ease. SenseTalk provides several different types of repeat loops for this purpose.
A repeat loop is used any time you want to execute one or more statements repeatedly some number of times. The statements to be repeated are preceded by one of the repeat statements described below, and must always be followed by an end repeat statement to mark the end of the loop. Repeat loops may be nested.
Repeat Forever
Behavior: This form of repeat loop repeats indefinitely until terminated. Usually, you do not really want your script to keep looping forever (a condition known by programmers as an “infinite loop”), so repeat forever loops typically include at least one exit, return, or pass statement that breaks out of the loop when some condition has been met: The word forever is optional.
Syntax:
repeat {forever}
statementList
end repeat
Example:
repeat forever
get nextValue(partList)
if it is empty then exit repeat // Generally there will be some handling within a forever repeat loop that provides a way to exit the repeat loop
TypeText it
end repeat
Repeat Number Times or For Duration
Behavior: This form repeats the number of times specified by the expression number or for the amount of time specified by the duration.
Syntax:
repeat {for} number {times}
statementList
end repeatrepeat {for} duration
statementList
end repeat
Example:
repeat 6 times
click "Arrow"
end repeat
Example:
repeat 1 minute
If imagefound(imageName:"Notification", waitFor:0) then
Logwarning "The warning is still present."
wait 2
else
Log "The warning is gone."
Exit Repeat
end if
end repeat
Repeat Until Condition, Repeat Until Time
Behavior: This form of repeat loop executes until the condition expression evaluates to a value of true, or until a given date/time arrives. The condition or time expression is evaluated before the first and each subsequent execution of the loop.
Syntax:
repeat until condition
statementList
end repeatrepeat until timeExpression
statementList
end repeat
Example:
repeat until list is empty
put the last item of list
delete the last item of list
end repeat
Example:
on ScrollUntilFound ImageName, myTime
Repeat until imagefound(image:imageName,waitfor:mytime)
if the counter is greater than 10 // Read more about the counter below
throw "image not found", imageName && "not found when scrolling."
end if
Typetext PageDown
Wait 1
end repeat
end ScrollUntilFound
Example:
repeat until "3:17 pm" // Repeats until the local system (eggPlant machine) clock hits 3:17 pm"
log "counting the seconds"
wait 1
end repeat
Repeat While
Behavior: This form of repeat loop will execute as long as the condition expression evaluates to a value of true. The condition is evaluated before the first and each subsequent execution of the loop.
Syntax:
repeat while condition
statementList
end repeat
Example:
set CountDown to 1000
repeat while CountDown is greater than 500
put readtext("TimerTL","TimerLR") into CountDown // Uses OCR to read a dynamic value off the SUT's screen
wait 3
end repeat
Example:
put 10 into x
repeat while x < 100
add x to x
end repeat
Repeat With Variable = Start to Finish
Behavior: This form of repeat sets a variable to each of a sequence of values. The term up to or down to may be used instead of the word to to indicate whether the loop variable should be incremented (increased) or decremented (decreased) each time through the loop. If neither direction is specified, up to is assumed. A step option lets you specify an amount other than 1 by which the loop variable is changed on each iteration.
Syntax:
repeat [with | for] variable [= | as | from] start {up | down} to finish {step stepAmount}
statementList
end repeat
Example:
put "Countdown"
repeat with n=10 down to 1
put n
wait one second
end repeat
put "BOOM!"
Example:
repeat with n=1 to the number of lines in file "/tmp/legalDoc"
put n & ": " & line n of file "/tmp/legalDoc"
end repeat
Example:
repeat with rate = 4.75 to 8.5 step 0.25
put "Rate: " & rate & tab &"Payment: " & calcPayment(rate)
end repeat
Example:
repeat with discount = 50 down to 0 step 5
insert (amount - discount%) after discountList
end repeat
The value of variable is set to the value of start before the first execution of the loop. Variable is then incremented (or decremented for “down to”) by the value of stepAmount (or by 1, if no stepAmount is given) before each subsequent repetition. The value of variable is compared to the value of finish before each execution. When variable is greater than finish (or less than finish for “down to”) the loop is terminated. If variable is a reference, it will be reset to an ordinary variable before use.
When a repeat loop using a loop variable is nested inside another, be sure to use a different variable for each loop, in order to avoid conflicts.
Repeat With Each
The repeat with each form is perhaps the most powerful and useful of all of SenseTalk’s repeat loops. This form of repeat makes it easy to step through each of the values in a list, the words in a sentence, the lines of text in a file, or many other subdivisions of a value.
When using this form to repeat through the items of a list, etc., the repeat variable (or it) will be set to each of the values in turn, from the first to the last. To step through the values in the opposite order, include the phrase in reverse order.
Syntax:
repeat with each subitemType [of | in] containingItem {in [reverse | reversed] order } {[as {a} | by] reference}
statementList
end repeatrepeat with variable = each subitemType [of | in] containingItem {in [reverse | reversed] order } {[as {a} | by] reference}
statementList
end repeatrepeat with each {subitemType} variable [of | in] containingItem {in [reverse | reversed] order } {[as {a} | by] reference}
statementList
end repeat
Example:
repeat with each item of myAccountList
set property balance of it to zero -- Set the balance of each account in a list of account objects to zero
end repeat
Example:
put empty into condensedList -- Start with nothing; gather all non-blank lines of a file into a container
put file "/tmp/somefile" into sourceText -- Read the file
repeat with each line of sourceText
if it is not empty then put it & return
after condensedList
end repeat -- Count the number of times each word occurs in a text file
Example:
answer file "Select a file to count its words" -- Get the file to be counted:
if the result is "Cancel" then exit handler
put it into sourceFile
put {:} into wordCounts -- Start with an empty property list
repeat with each word of file sourceFile
add 1 to property (it) of wordCounts
end repeat
Example:
put "Word counts in file " & sourceFile & ":"
repeat with each item of the keys of wordCounts
put it && "appears" && wordCounts.(it) && "times"
end repeat
Example:
repeat with each line of file "/tmp/example" by reference
if it begins with "#" then delete it -- Delete every line that begins with "#" in a text file:
end repeat
Example:
put files(SuiteInfo().imagesFolder & slash & "OKButton") into imageFiles // Stores a list of objects representing all of the files in the image collection named "OKButton"
delete each item of imageFiles which ends with ".imageinfo" // remove imageinfo files from the list
repeat with each image of ImageFiles // display a list of images and their size on disk
put image && ">>>" && the size of image
end repeat