Repeatループ

コンピュータの卓越した強みの1つに、繰り返しタスクを簡単に実行できることがあります。SenseTalkはこの目的のために、様々なタイプの繰り返しループを用意しています。

repeat(繰り返し)ループは、1つ以上のステートメントを何回か繰り返したい場合に使用します。繰り返すステートメントの前には、以下に示すいずれかのrepeatステートメントを記述し、最後に必ずend repeatステートメントを付けてループの終わりを示す必要があります。Repeatループはネスト化することができます。

repeat forever

挙動:この形式のrepeatループは、止めるまで無限にループを実行します。通常、ユーザーにとってスクリプトの無制限のループ(プログラマーの間で「無限ループ」として知られる状態)は好ましくないため、repeat foreverループは一般的に、いくつかの条件が整った場合にループから抜け出す少なくとも1つのステートメント(exitreturn、またはpassステートメント)を含みます。単語のforeverはオプションです。

構文:

repeat {forever}

statementList

end repeat

例:

repeat forever

get nextValue(partList)

if it is empty then exit repeat // 通常、forever repeatループには、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

挙動:この形式のrepeatループは、条件式が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 repeatindex is greater than 10 // 以下のrepeatIndexに関する詳細を確認します

throw "image not found", imageName&&"not found when scrolling."

end if

Typetext PageDown

Wait 1

end repeat

end ScrollUntilFound

例:

repeat until "3:17 pm" // ローカルシステム(eggPlantマシン)の時計が3:17 pmを指すまで繰り返します

log "counting the seconds"

wait 1

end repeat

repeat while condition

挙動:この形式のrepeatループは、条件式がtrueと評価される限りループを実行します。条件は、ループの最初の実行前とその後の実行前に毎回評価されます。

構文:

repeat while condition

statementList

end repeat

例:

set CountDown to 1000

repeat while CountDown is greater than 500

put readtext("TimerTL","TimerLR") into CountDown // OCRを利用してSUT画面から動的値を読み取ります

wait 3

end repeat

例:

put 10 into x

repeat while x < 100

add x to x

end repeat

repeat with variable = start to finish

挙動:この形式の繰り返しは、変数を連続する値に設定します。単語toの代わりに用語up toまたはdown 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

技術トピック

変数の値は、ループの最初の実行前に開始値に設定されます。そのため、その後の各繰り返しの前に、変数はstepAmountの値(stepAmountを指定していない場合は1)だけ増加(「down to」については減少)します。変数の値は、繰り返しの実行前に毎回終了値と比較されます。変数が終了値よりも大きい(「down to」については小さい)場合は、ループは終了します。変数が参照の場合は、使用前に通常の変数に再設定されます。

ヒント:ループ変数を使用しているrepeatループを別のループ内でネスト化する場合は、混乱を避けるために、ループごとに異なる変数を使用する必要があることに注意してください。

repeat with each

このrepeat with eachの形式は、SenseTalkのすべてのrepeatループの中でおそらく最も強力で役に立つループです。この形式の繰り返しは、リスト内の各値、文章中の各ワード、ファイル内のテキストの各行、またはその他多くの値の区分をステップスルーすることを容易にします。

構文:

repeat with each subitemType [of | in] containingItem {by reference}

statements

end repeat

構文:

repeat with variable = each subitemType [of | in] containingItem {by reference}

statements

end repeat

構文:

repeat with each {subitemType} variable [of | in] containingItem {by reference}

statements

end repeat

例:

repeat with each item of myAccountList

set property balance of it to zero -- アカウントオブジェクトのリスト内の各アカウントのバランスを0に設定します

end repeat

例:

put empty into 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 -- 空のプロパティリストから開始します

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」と命名された画像コレクション内に、全ファイルを表すオブジェクトのリストを格納します

repeat with each item of ImageFiles

if it contains ".imageinfo" then delete it from ImageFiles // ファイルオブジェクトのテキスト表現に文字列「.imageinfo」が含まれるかどうかをチェックし、含まれる場合はリストからそのアイテムを削除します

end repeat

例:

set the RemoteWorkInterval to .1

repeat with each item of everyimagelocation("CheckBox")

click it

end repeat

set the RemoteWorkInterval to .7

技術トピック

これらの繰り返しフォーマットは、指定したcontainingItemに含まれるオブジェクトごと、または部分ごと(subitemTypeの指定による)に、repeatループ内のステートメントを(マッチするend repeatステートメントまで)1回だけ実行します。実際には、number関数が許可するsubitemTypecontainingItemのあらゆる組み合わせを使用できます(すなわち、オブジェクトやコンテナ内でカウント可能なあらゆるものを使用できます)。

repeat with eachステートメントのすべての形式において、subitemTypeは反復処理されるアイテムのタイプを指定し(上に示した3つ目のフォーマットで省略される場合は、「item」が想定される)、containingItemはその一部が反復処理される特定のオブジェクトまたはコンテナとなります。

2つ目と3つ目のフォーマットにおいて、variableは、repeatループの実行ごとに特定のサブアイテムの識別子またはコンテンツを割り当てられる変数の名前となります。最初のフォーマットで特定の変数を指定しない場合は、変数itが使用されます。by referenceを指定する場合は、variableは各サブアイテムの参照となるように設定します。あるいはvariable(またはit)が既に参照である場合は、ループ変数として使用する前に通常の変数となるよう再設定されます。

subitemTypeがオブジェクトのタイプである場合は、ループの実行ごとに、特定のsubObjectのロングIDがitまたはvariableに割り当てられます。subitemTypeがテキストチャンクのタイプである場合は、テキストの実際のチャンクは、itまたはvariableに格納されます。

ヒント:ループ変数を使用しているrepeatループを別のループ内でネスト化する場合は、ループごとに異なる変数を使用する必要があることに注意してください。特に、変数をループ変数として暗黙のうちに使用するネスト化された繰り返しに注意してください。

repeat at least once until、repeat at least once while

挙動:条件のチェック前に、repeatループ内のステートメントを最低1回実行します。

構文:

repeat at least once until condition

repeat at least once while condition

例:

repeat at least once until list is empty

put the last item of list

delete the last item of list

end repeat

例:

// 以下のようなコードを使用して、ページまたは画面の下までスクロールした時期を検出します

put RemoteScreenSize() into DUTScreenSize // SUT画面のサイズを格納します

put (.25*DUTScreenSize.x, .25*DUTScreenSize.y, .75*DUTScreenSize.x, .75*DUTScreenSize.y) into ClippingRectangle

CaptureScreen (Name: "state", Rectangle: ClippingRectangle) // 画面の中央部分のみでSUTのスクリーンショットをキャプチャします

put the result into refImage //CaptureScreen画像のファイルパスを変数に格納します

repeat at least once while not imageFound(image:refImage,searchRectangle:ClippingRectangle,waitFor:0) // 条件がすぐに満たされる場合でも、repeatループ内のステートメントが最低1回実行されるように強制します

CaptureScreen (Name: "state", Rectangle: ClippingRectangle) // SUTの新しいスクリーンショットをキャプチャします

put the result into refImage

TypeText PageDown // 画面をページの下方にスクロールします

Wait 2.5 // スクロールアクションが「落ち着く」まで十分な時間待機します

end repeat

counter関数、repeatIndex関数

挙動:counter()関数またはrepeatIndex()関数は、最も内側で実行されるrepeatループの現時点での反復回数を返します。本アプローチは、ループの実行回数を記録したり、特定の回数に達した後にアクションを実行した回数を記録するのに役立ちます。

構文:

counter()

the counter

repeatindex()

the repeatIndex

例:

repeat for 3 minutes

if the counter is a multiple of 100 then put the counter

end repeat

例:

repeat until imagefound(imagename:"NextButton",waitFor:0)

if repeatindex() = 2 then throw "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パラメータに関する値が渡されたことを確認し、値が渡されていなければ例外を投げます

put the width of imageSize(myImage) into XDirection // 画像幅のピクセル数を決定します

put the height of imageSize(myImage) into YDirection

set TotalPixels to XDirection times YDirection // キャプチャした画像内のトータルのピクセル数を計算します

set ColorPixels to 0

repeat YDirection times

put the repeatIndex - 1 into YCoord // repeatindexを使用して、操作中の座標を追跡します

repeat XDirection times

put the repeatIndex - 1 into XCoord

put imagecoloratlocation(myImage, (XCoord,YCoord)) into 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

ヒント:repeatIndex()は、あらゆるタイプのrepeatループで使用できます。この値は、ループの最初の実行時に1に設定され、反復ごとに1ずつ増加します。ネスト化されたループ内部で本関数を使用する場合は、本関数が存在する最も内部のループのインデックスを報告します。

 

This topic was last updated on 2月 01, 2019, at 11:13:23 午前.

Eggplant icon Eggplant.io | Documentation Home | User Forums | Support | Copyright © 2019 Eggplant