メインコンテンツまでスキップ

繰り返しループ

コンピュータの大きな強みの一つは、繰り返しタスクを容易に実行する能力です。この目的のために、SenseTalkはいくつかの異なる種類の繰り返しループを提供します。

repeatループは、一つ以上のステートメントを何回か繰り返し実行したいときに使用されます。繰り返すステートメントは以下に述べるrepeatステートメントの一つによって前置され、常にループの終了を示すend repeatステートメントによって後置される必要があります。繰り返しループはネストすることができます。

Repeat Forever

動作: この形式の繰り返しループは、終了するまで無限に繰り返します。通常、あなたはスクリプトが永遠にループし続けること(プログラマーにとっては「無限ループ」として知られている状態)を本当に望んでいませんので、repeat foreverループは通常、少なくとも一つのexitreturn、またはpassステートメントを含み、何らかの条件が満たされたときにループから抜け出します。foreverという単語はオプションです。

構文:
repeat {forever}
     statementList
end repeat

例:

repeat forever
get nextValue(partList)
if it is empty then exit repeat // 一般的には、永遠に繰り返すループの中には、繰り返しループを終了する方法を提供する何らかのハンドリングが存在します。
TypeText it
end repeat

Repeat Number Times または For Duration

動作: この形式は、numberの表現によって指定された回数、またはdurationによって指定された時間の量のために繰り返します。

構文:
repeat {for} number {times}
     statementList
end repeat

repeat {for} duration
     statementList
end repeat

例:

repeat 6 times
click "Arrow"
end repeat

例:

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

動作: この形式の繰り返しループは、条件式がtrueの値に評価されるまで、または指定された日時が到来するまで実行されます。条件または時間の式は、ループの最初とその後の各実行前に評価されます。

構文:
repeat until condition
     statementList
end repeat

repeat until timeExpression
     statementList
end repeat

例:

repeat until list is empty
put the last item of list
delete the last item of list
end repeat

例:

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

例:

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

end repeat

end repeat

文法:
repeat at least once until condition
     statementList
end repeat

例:

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

例:

put 10 into x
repeat while x < 100
add x to x
end repeat

Repeat With Variable = Start to Finish

動作: この形式の繰り返しは、変数を一連の値のそれぞれに設定します。 up toまたはdown toの用語は、ループ変数がループを通じて増分(増加)するか、減分(減少)するかを示すために、toの代わりに使用できます。 どちらの方向も指定されていない場合、up toが想定されます。 stepオプションは、ループ変数が変更される他の1以外の量を指定することができます。

構文:
repeat [with | for] variable [= | as | from] start {up | down} to finish {step stepAmount}
     statementList
end repeat

例:

put "Countdown"
repeat with n=10 down to 1
put n
wait one second
end repeat
put "BOOM!"

例:

repeat with n=1 to the number of lines in file "/tmp/legalDoc"
put n & ": " & line n of file "/tmp/legalDoc"
end repeat

例:

repeat with rate = 4.75 to 8.5 step 0.25
put "Rate: " & rate & tab &"Payment: " & calcPayment(rate)
end repeat

例:

repeat with discount = 50 down to 0 step 5
insert (amount - discount%) after discountList
end repeat
Tech Talk

_variable_の値は、ループの最初の実行前に_start_の値に設定されます。その後、_variable_は各後続の繰り返し前に_stepAmount_の値(または_stepAmount_が指定されていない場合は1)で増分(または“down to”の場合は減分)されます。各実行前に_variable_の値と_finish_の値が比較されます。_variable_が_finish_より大きい(または“down to”の場合は_finish_より小さい)場合、ループは終了します。_variable_が参照である場合、使用前に通常の変数にリセットされます。

先端

ループ変数を使用する繰り返しループが別の中にネストされている場合、各ループで異なる変数を使用することを確認してください。これにより、競合を避けることができます。

Repeat With Each

repeat with each形式は、おそらくSenseTalkのすべての繰り返しループの中で最も強力で便利なものです。この形式の繰り返しは、リストの値、文章の単語、ファイルのテキスト行など、値の多くのサブディビジョンを通じて各値を簡単にステップすることができます。

この形式を使用してリストのアイテムなどを繰り返すと、繰り返し変数(またはit)は、最初から最後までの値のそれぞれに設定されます。逆の順序で値をステップするには、フレーズin reverse orderを含めます。

[**構文:**md repeat with each subitemType [of | in] containingItem {in [reverse | reversed] order } {[as {a} | by] reference}
     statementList
end repeat

repeat with variable = each subitemType [of | in] containingItem {in [reverse | reversed] order } {[as {a} | by] reference}
     statementList
end repeat

repeat with each {subitemType} variable [of | in] containingItem {in [reverse | reversed] order } {[as {a} | by] reference}
     statementList
end repeat

例:

repeat with each item of myAccountList
set property balance of it to zero -- アカウントオブジェクトのリストの各アカウントの残高をount objects to zero
end repeat

例:

condensedListに空を入れる -- 最初は何もなし; ファイルのすべての非空行をコンテナに集める
put file "/tmp/somefile" into sourceText -- ファイルを読む
repeat with each line of sourceText
if it is not empty then put it & return
after condensedList
end repeat -- テキストファイルにおける各単語の出現回数を数える

例:

answer file "Select a file to count its words" -- カウントされるファイルを取得:
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

例:

put "Word counts in file " & sourceFile & ":"
repeat with each item of the keys of wordCounts
put it && "appears" && wordCounts.(it) && "times"
end repeat

例:

repeat with each line of file "/tmp/example" by reference
if it begins with "#" then delete it -- テキストファイル内の"#"で始まるすべての行を削除:
end repeat

例:

put files(SuiteInfo().imagesFolder&slash&"OKButton") as a list into imageFiles // "OKButton"という名前のイメージコレクション内のすべてのファイルを表すオブジェクトのリストを格納します
delete each item of imageFiles which ends with ".imageinfo" // リストからimageinfoファイルを削除します。
ImageFilesの各アイテムを繰り返す
put image && ">>>" && the size of image
end repeat

例:

set the RemoteWorkInterval to .1
repeat with each item of everyimagelocation("CheckBox")
click it
end repeat
set the RemoteWorkInterval to .7
Tech Talk

これらの繰り返し形式は、繰り返しループ内の文(対応するend repeat文まで)を、指定された_containingItem_内に含まれる各オブジェクトや部分(_subitemType_で指定された)に対して正確に一度実行します。number関数で許される_subitemType_と_containingItem_の組み合わせのほとんど全てを使用できます(つまり、何かの含まれるオブジェクトやコンテナ内で数えられるもの全て)。

repeat with each文の全ての形式において、_subitemType_は反復処理される項目の種類を指し、_containingItem_は反復処理される部分を含む具体的なオブジェクトやコンテナを指します(上記の第3形式で省略された場合、"item"が仮定されます)。

第二および第三の形式では、_variable_は反復ループの都度、特定のサブアイテムの識別子または内容が割り当てられる変数の名前です。第一の形式では、特定の変数が指定されていない場合、変数itが使用されます。by referenceが指定されている場合、_variable_は各サブアイテムへの参照として設定されます。それ以外の場合、variable(またはit)がすでに参照である場合、それはループ変数として使用される前に通常の変数にリセットされます。

_subitemType_がオブジェクトのタイプである場合、特定のサブオブジェクトの長いIDがループの都度itまたは_variable_に割り当てられます。_subitemType_がテキストチャンクのタイプである場合、実際のテキストチャンクがitまたは_variable_に格納されます。

先端

ループ変数を使用するrepeatループが別の内部にネストされている場合、各ループに対して別の変数を使用してください。特に、itをループ変数として暗黙的に使用するネストされた繰り返しに注意してください。

Repeat At Least Once Until, Repeat At Least Once While

動作: 条件をチェックする前にrepeatループ内の文を一度実行し、文が少なくとも一度は実行されることを保証します。

関数として:
repeat at least once until condition
     statementList
end repeat

repeat at least once while condition
     statementList
end repeat

例:

リストが空になるまで少なくとも一度は繰り返す
リストの最後のアイテムを出力する
リストの最後のアイテムを削除する
end repeat

例:

// ページ/画面の最下部までスクロールしたときに検出するには、以下のようなコードを使用します。
RemoteScreenSize()をDUTScreenSizeに入れる // SUTの画面サイズを保存する
(.25*DUTScreenSize.x, .25*DUTScreenSize.y, .75*DUTScreenSize.x, .75*DUTScreenSize.y)をClippingRectangleに入れる
CaptureScreen (Name: "state", Rectangle: ClippingRectangle) // SUTのスクリーンシンショットをキャプチャする、画面の中央部分のみ
結果をrefImageに入れる // CaptureScreenイメージのファイルパスを変数に保存する
refImageが見つからない、searchRectangle:ClippingRectangle,waitFor:0がない間は少なくとも一度は繰り返す // 条件がすぐに満たされても、repeatループ内の文が少なくとも一度は実行されるようにする
CaptureScreen (Name: "state", Rectangle: ClippingRectangle) // SUTの新しいスクリーンショットをキャプチャする
結果をrefImageに入れる
TypeText PageDown // ページの下部に向かって画面をスクロールする
Wait 2.5 // スクロールアクションが"落ち着く"のに十分な時間を待つ
end repeat

Counter Function, RepeatIndex Function

動作: counterまたはrepeatIndex関数は、最も内側の実行中のrepeatループの現在の反復回数を返します。このアプローチは、ループが何回反復したか、またはあるカウントが達成されたときに何回アクションを取ったかを追跡するのに役立ちます。

文法:
counter()
the counter
repeatIndex()
the repeatIndex

例:

3分間繰り返す
カウンタが100の倍数であればカウンタを出力する
end repeat

stk-syntax-definition.md

imagename:"NextButton",waitFor:0が見つかるまで繰り返す
repeatindex() = 2ならば、"Image not found", "Image not found while scrolling."を投げる
SwipeUp
wait 2
end repeat

構文:

function FindColorPercentage myImage,myColor // myImageとmyColorの2つのパラメーターを持つカスタム関数FindColorPercentageを宣言する
assert that myColor is not empty // myColorパラメーターに値が渡されたことを確認し、渡されなかった場合は例外をスローする
imageSize(myImage)の幅をXDirectionに入れる // 画像のピクセル幅を決定する
imageSize(myImage)の高さをYDirectionに入れる
TotalPixelsをXDirectionとYDirectionの積に設定する // キャプチャされた画像の総ピクセル数を計算する
ColorPixelsを0に設定する
YDirection回繰り返す
カウンター - 1をYCoordに入れる // カウンターを使用して操作している座標を追跡する
XDirection回繰り返す
カウンター - 1をXCoordに入れる
imagecoloratlocation(myImage, (XCoord,YCoord))をReturnedColorに入れる // imageColoratLocation()関数を使用して現在のピクセルのRGB値を取得する
if ReturnedColor is color(myColor) then add 1 to ColorPixels // color()関数を使用して、実際のRGB値をFindColorPercentage関数に渡されたRGB値と比較する
end repeat
end repeat
return round((ColorPixels/TotalPixels) * 100, 1) // 欲しい色と一致するピクセルの割合を計算する
end function
先端

任意のタイプのrepeatループでcounterを使用します。その値はループを最初に通過したときに1に設定され、各反復で1ずつ増加します。ネストされたループ内で使用される場合、それが存在する最も内側のループのインデックスを報告します。