Flow Control 流れ制御

Overview

Task flow control is based on the following Tasker elements:

  • variable values
  • conditions on individual actions
  • If / Else / Endif actions for conditional grouping of following actions
  • For / End For to do a set of actions once for each of a set of elements
  • Goto action (jumping around within a task).
  • Perform Task action (calling other tasks as subroutines)
  • Stop action (terminate task immediately)

On the Wiki there is a detailed example of processing a file’s content [External Link].

Tip: if you accidentally create a task that never ends when experimenting with loops, use the Kill button in the Task Edit screen to end it manually.

https://tasker.joaoapps.com/userguide/en/flowcontrol.html

Taskerにおける流れ制御は以下の要素によって決まります。

  • 変数の値。
  • 個々のアクションの状態
  • 複数のアクションをグループ化する If(イフ)/ Else(エルス)/ Endif(エンドイフ)の条件判定。
  • For / End Forで要素群に対して一連のアクションを一度実行する。
  • Gotoアクション(タスク内の任意の場所へのジャンプ)。
  • Perform Taskアクション(サブルーチンとして他のタスクを実行)。
  • Stopアクション(タスクの即時停止)。

Taskerウィキにはファイルの内容の実行について詳しい例があります。(外部リンク)

一口メモ:アクションのループを試している時に無限ループに陥ってしまった場合は、タスク編集画面のキルボタンで強制停止させる事ができます。

Conditions(条件)

Every action can have a condition associated with it (specify this in the Action Edit screen). If the condition does not match, the action will be skipped.

A condition consists of an operator (‘equals’ etc) and two parameters. The possible operators are:

  • Equals (eq)
    The left parameter is identical to the right parameter.
  • Doesn’t Equal (neq)
    The left parameter is not identical to the right parameter.
  • Matches (~)
    The right parameter is a pattern which the left parameter is
    matched against.
  • Not Matches (!~)
    As above, but the match must fail for the action to be executed.
  • Matches Regex (~R)
    The right parameter is a regular expression which the left parameter is matched against.
  • Doesn’t Match Regex (!~R)
    As above, but the match must fail for the action to be executed.
  • Maths: Less Than (<)
    Both parameters (after variables are substitued) must be numbers or mathematical expressions and the first must be less than the second e.g. 3 < 6. See Maths for more info.
  • Maths: Greater Than (>)
    As above, but the first parameter must evaluate to more than the second.
  • Maths: Equals (=)
    As above, but the two parameters must be numerically equal.
  • Maths: Isn’t Equal To (!=)
    As above, but the two parameters must be not numerically equal.
  • Maths: Is Even (Even)
    The left parameter is an even number.
  • Maths: Is Odd (Odd)
    The left parameter is an odd number.
  • Is/Isn’t Set (Set/!Set)
    Whether the specified variable has a value or not.

Expressions which are not mathematically valid e.g. I Am The Walrus > 5 give a warning and evaluate to false when used with a mathematical operator.

https://tasker.joaoapps.com/userguide/en/flowcontrol.html

全てのアクションにはアクションの実行に付随した条件を(アクション編集画面から)設定することができます。設定した条件が不成立だった場合にはそのアクションは飛ばして次のアクションに移ります。

各条件は演算子(イコール記号など)と2つのパラメーターから成ります。指定可能な演算子は以下の通りです。

  • 等しい(eq)
    左側のパラメーターが右のパラメーターと同等である場合。
  • 等しくない(neq)
    左側のパラメーターが右のパラメーターと同等でない場合。
  • 一致(~)
    右側のパラメーターは左のパラメーターに対してマッチすべきパターンを指定します。
  • 不一致(!~)
    条件判定は「一致」と同じですが、一致した場合に条件が不成立に成るという点が一致とは異なります。
  • 正規表現による一致(~R)
    右側のパラメーターは左のパラメーターに対してマッチすべき正規表現のパターンです。
  • 正規表現による不一致(!~R)
    条件判定は「正規表現による一致」と同じですが、一致した場合に条件が不成立に成るという点が正規表現による一致とは異なります。
  • 数学:より小さい(<)
    左右のパラメーター(変数の場合はその内容)は数値または数式でなければなりません。左側のパラメーターが右よりも小さい場合に条件が成立します。詳細は数学の項目をご覧下さい。
  • 数学:より大きい(>)
    パラメーターの内容は「より小さい」と同じですが、左側のパラメーターが右よりも大きい場合に条件が成立します。
  • 数学:等しい(=)
    パラメーターの内容は「より小さい」と同じですが、左右のパラメーターが数値として等しい場合に条件が成立します。
  • 数学:等しくない(!=)
    パラメーターの内容は「より小さい」と同じですが、左右のパラメーターが数値として等しくない場合に条件が成立します。
  • 数学:偶数(Even)
    左側のパラメーターが偶数の場合に条件が成立します。
  • 数学:奇数(Odd)
    左側のパラメーターが奇数の場合に条件が成立します。
  • セットされている/セットされていない(Set/!Set)
    指定された変数に値が入っているか、あるいはいないかによって条件が成立します。

数学的演算子を選択しているにもかかわらず条件式が数学的に妥当ではない場合、例えば、「私はセイウチ > 5」というような数式は警告を表示した上で不成立と判定されます。

Foreach Loop(Foreachによる繰り返し)

Goal: perform a set of actions for each of apple, pear and banana.

1.For
%item
apple,pear,banana
Loop once for each of apple, pear and banana
2.  Action OneExample: Flash %item
3.  Action Two
4.End For
Return to action 1 if we havn’t done all the items yet

Result: Action One and Action Two are performed three times. The first time, the variable %item is set to apple, the second time pear and the last time banana.

https://tasker.joaoapps.com/userguide/en/flowcontrol.html

目的:「りんご」、「なし」、「バナナ」のそれぞれに対して一連のアクションを適用する。

1.For
%item
apple,pear,banana
「りんご」、「なし」、「バナナ」に対して繰り返し処理を開始。
2.  Action One例:%itemをフラッシュ表示する。
3.  Action Two何かしらのアクション。
4.End For
「りんご」、「なし」、「バナナ」の全てに対して2.3.のアクションが
実行されていなければ1.へ戻る。

実行結果:繰り返し処理の結果、Action OneとAction Twoは三回繰り返し実行され、1回目は%item変数には「りんご」がセットされ、2回目は「なし」、3回目は「バナナ」がそれぞれセットされます。

You can insert a Goto action in the loop with either Top of Loop (meaning continue, skip to the next item straight away) or End of Loop (meaning break, stop without doing any more items) specified.

In adition to simple text, the For action accepts any comma-separated combination of these Items:

  • a numeric range e.g. 1:5 (= 1,2,3,4,5)
  • a numeric range with a jump e.g. 8:2:-2 (= 8,6,4,2)
  • a numeric range defined by variable values e.g. 2:%end:%skip, 1:%arr(#)
  • a variable name (which is replaced) e.g. %fruit (= banana maybe)
  • a variable array part e.g. %arr(1:2) (= %arr1, %arr2 = apple,banana maybe)

A common case is to use %arr(), which performs a loop for each element in the array %arr.

Warning: the Values parameter in the loop is reevaluated with each iteration of the loop, meaning that modifying array variables which appear there from within the loop can have unexpected effects. To workaround that, it can be best to use the following sequence:

   Variables Set, %values, %arrayWhichWillChange()
   Variable Split, %values
   For, %value, %values()
      ...
https://tasker.joaoapps.com/userguide/en/flowcontrol.html


繰り返し処理の途中にGotoアクションを入れて繰り返しの開始に戻る(つまり、それ以降の繰り返し処理の内容を端折って次の繰り返しを始めるということです)か、繰り返しの最後に進む(つまり、残りの繰り返し処理を全てキャンセルして繰り返しを抜けるということです)ことができます。

シンプルなテキストに加え、Forアクションでは以下のようなコンマ区切りの様々なデータを指定することができます。

  • 数値範囲。例:1:5 (=1,2,3,4,5)
  • 増分指定付きの数値範囲。例:8:2:-2 (=8,6,4,2)
  • 変数による数値範囲。例:2:%end:%skip, 1:%arr(#)
  • 変数名(格納された値)。例:%fruit (= バナナなど)
  • 配列変数。例:%arr(1:2) (=%arr1, %arr2 = りんご、バナナなど)

一般的な場合%arr()を使います。これは配列変数%arrの各要素について繰り返し処理を行います。

注意:繰り返し処理内のvaluesパラメーターは繰り返しごとに再計算されます。したがって繰り返し処理内でその配列変数に変更を加えると予期せぬ結果となる場合があります。これを避けるためには次のような手順が有効です。

   Variables Set, %values, %arrayWhichWillChange()
   Variable Split, %values
   For, %value, %values()
      ...

For Loop(Forによる繰り返し)

Goal: perform a set of actions for each of a set of elements in turn. Use the Foreach Loop as described above, with the Items parameter being a range specification e.g. 4:0, 100, 0:8:2 (= 4,3,2,1,0,100,0,2,4,6,8).

https://tasker.joaoapps.com/userguide/en/flowcontrol.html

目的:繰り返しごとに対象の要素に対して一連のアクションを適用します。前述のForeachによる繰り返しをItemパラメーターに繰り返し回数を指定して実行します。例:4:0, 100, 0:8:2 (=4,3,2,1,0,100,0,2,4,6,8)

Until Loop(実行後判定による繰り返し)

Goal: perform a Task X until some condition is met (at least once)

1.Action One
2.Action Two
3.Goto
1
If %qtime < 20
Return to action 1 if runtime < 20

Result: Action One and Action Two are performed until %QTIME contains the value 20 or more i.e. until the task has been running for 20 seconds.

Note: %QTIME is a builtin local variable available in all tasks.

https://tasker.joaoapps.com/userguide/en/flowcontrol.html

目的:あるタスクを条件が成立するまで繰り返し実行します(少なくとも一回は実行します)。

1.Action One
2.Action Two
3.Goto
1
If %qtime < 20
実行時間 < 20であればAction Oneに戻る

結果:Action OneとAction Twoは%qtime変数に20以上の数値が格納されるまで繰り返し実行されます。つまり、20秒間の間タスクが繰り返されます。

注意:%qtime変数は全てのタスクから参照可能な組み込みローカル変数です。

While Loop(実行前判定による繰り返し)

Goal: perform a Task X while some condition is met.

1.Stop

If %fruit Not Matches Apple
Stop task if it’s not crunchy, otherwise go to next action
2.Action One
3.Action Two
4.Goto
1
Go back and see if we’re still crunchy

Result: Action One and Action Two are performed while %fruit contains the value Apple.

https://tasker.joaoapps.com/userguide/en/flowcontrol.html

目的:条件が成立すればタスクを繰り返します。

1.Stop

If %fruit Not Matches Apple
条件が成立すればタスクを停止します。そうでなければ次のアクションを実行します。
2.Action One
3.Action Two
4.Goto
1
繰り返しの最初に戻り条件の判定を行います。

結果:Action OneとAction Twoは%fruit変数に文字列”Apple”が格納されている限りタスクを繰り返します。

Counter Loop(回数による繰り返し)

Goal: perform a Task X a set number of times.

1.Variable Set
%count, 0
Initialize the counter
2.Action One
Label: LoopStart
3.Action Two
4.Variable Add
%count, 1
Add one to %count
5.Goto
LoopStart
If %count < 10
Return to action 2 if count < 10

Result: after initialization of %count to 0, the task loops around the actions from 2-5 until %count reaches 10, at which point the condition on the Goto fails and the end of the task is reached.

Note that we used a Goto to a labelled action this time. In all but the very simplest tasks it’s better to use a label rather than a number. It’s easier to work out what’s happening and if you insert or delete actions before the loop starts, the Goto will still jump to the right place.

An alternative way to do this loop is to use a For action specified as 0:10.

https://tasker.joaoapps.com/userguide/en/flowcontrol.html

目的:タスクを指定された回数繰り返します。

1.Variable Set
%count, 0
カウンターを初期化します。
2.Action One
Label: LoopStart
3.Action Two
4.Variable Add
%count, 1
%count変数に1を加えます。
5.Goto
LoopStart
If %count < 10
カウントが10未満ならばaction 2へ戻ります。

結果:%count変数を0に初期化後2.から5.までのアクションを%count変数に10が格納されるまで繰り返します。10に達した時点でGotoアクションの実行条件が不成立となり繰り返し処理を終了します。

この例ではGotoアクションの飛び先にラベル付きアクションを指定している点に注意してください。ごく単純なタスクを除いては行番号よりもラベルを使うことをお勧めします。

この例と同様の繰り返しをする別の方法として、Forアクションの繰り返しを0:10に指定しても良いです。

If / Then / Else Condition(If / Then / Else条件分岐)

Goal: perform certain Tasks if conditions are met, otherwise perform a different task.

1.If
%fruit ~ Apple
~ is short for ‘matches’
2.  Action One
3.  Action Two
4.Else If
%fruit ~ Pear
an Else action with a condition
5.  Action Three
6.Else
7.  Action Four

Result: actions One and Two are executed if %fruit matches Apple, Action Three is executed if %fruit matches Pear, otherwise Action Four is executed.

Notes:

  • you can have as many Else Ifs in a condition as you like
  • if your condition is in the middle of a more complicated task, you need to tell Tasker where the condition ends with an End If action
https://tasker.joaoapps.com/userguide/en/flowcontrol.html

目的:条件が成立したら特定のタスクを実行します。不成立の場合は別のタスクを実行します。

1.If
%fruit ~ Apple
~は「一致」の省略表記です。
2.  Action One
3.  Action Two
4.Else If
%fruit ~ Pear
条件によって別のアクションを実行します。
5.  Action Three
6.Else
7.  Action Four

結果:Action OneとAction Twoは%fruit変数に文字列”Apple”が格納されている場合に実行されます。Action Threeは”Pear”の場合に実行されます。それ以外の場合はAction Fourが実行されます。

注意:

  • Elseは必要なだけ条件分岐に含めることができます。
  • より複雑なタスクの中で条件分岐を行う場合は、条件分岐の終わりをEnd Ifアクションで明示する必要があります。

Subroutines(サブルーチン)

To call another task, use the Perform Task action. To use it as a subroutine, you just need to ensure that the priority of the calling task is less than the priority of the called task (more info: scheduling).

The parent can optionally pass values to the child and receive a result back:

Parent Task

1.  Perform Task
Child,
Priority, 10
%par1, 5,
Result Value Variable, %result
pass 5 to the child, expect a result in %result
2.  Variable Flash
Result: %result
what did we get back ?

Child Task

1.  Variable Set
%newval, %par1 + 1, Do Maths
add one to the value that was passed
1.  Return
%newval
set %result in the parent to the value of %newval in the child

Result: the parent flashes 6

Notes:

  • changes made to %par1 and %par2 in the child task are not reflected by their changing in the parent task
  • receiving a return value is optional for the parent, even if the child tries to give it one
  • unlike Return statements in most computer languages, Tasker’s does not necessarily stop the child task, so if the child and parent have the same priority they can both run together and the child return several results over time.
https://tasker.joaoapps.com/userguide/en/flowcontrol.html

他のタスクを呼び出す場合はPerform Task(タスクの実行)アクションを使います。このアクションをサブルーチンとして使う場合、呼び出された側のタスク(子タスク)は呼び出した側のタスク(親タスク)よりも常に低い優先度で実行されることを覚えておいてください。(詳しくはスケジューリングを参照)

必要であれば親タスクから子タスクに引数を渡すことができ、また子タスクから戻り値を受け取ることもできます。

親タスク

1.  Perform Task
Child,
Priority, 10
%par1, 5,
Result Value Variable, %result
子タスクに引数5を渡します。%resultに戻り値が格納されます。
2.  Variable Flash
Result: %result
戻り値をフラッシュ表示します。

子タスク

1.  Variable Set
%newval, %par1 + 1, Do Maths
渡された引数に1を加えます。
1.  Return
%newval
子タスクの%newval変数に格納されている実行結果を親タスクの%result変数に格納します。

結果:親タスクは6をフラッシュ表示します。

注意:

  • 子タスクの変数%par1に加えられた変更は親タスクには影響しません。
  • 子タスク側で戻り値を返してもそれを親タスク側で受け取るかどうかは任意です。
  • 多くのコンピューター言語とは異なり、Returnアクションによって子タスクの実行が終了することはありません。従って子タスクが親タスクと同じ優先度で実行されている場合、子タスクは実行中に複数の結果を返すことがあります。