WHEN <string1> <string2>
ENDWHEN <string1>

The WHEN command, of which up to ten may be current at any
one time, causes Scripta to scan all data received from
the remote computer, as it arrives. Whenever <string1> is
received, <string2> is transmitted back.

This scanning of received data continues until the command

                  ENDWHEN <string1>

is executed, specifying the same <string1> as used in the
WHEN command.

ENDWHEN with no parameters 'ends' all currently active
WHEN commands.

Note that scanning for WHEN strings runs parallel to
scanning for WAIT and SENDWAIT strings. If a WHEN string
is encountered, Scripta sends the return string but then
resumes the wait for the currently outstanding WAIT or
SENDWAIT string.

The difference between WHEN and the WAIT and SENDWAIT
commands is that it doesn't matter if a WHEN string never
arrives.

When you issue a (SEND)WAIT command, script processing
ceases until the designated string is received. When you
issue a WHEN command, Scripta notes the WHEN strings but
continues script processing as normal.

In plain English, WAIT and SENDWAIT are 'main stream'
script commands, used to detect and react to the main
points in the remote computer's conversation. WHEN says to
Scripta, 'carry on processing the script but, as well as
any WAIT or SENDWAIT commands I might issue, please also
react to this extra string if you receive it from the
remote computer'.

WHEN is very useful for detecting strings which are either
sent a variable number of times or are sometimes not sent
at all.

For instance, one service that I use asks you to enter
your date of birth every tenth time that you log in. It
would be no good issuing the command

              WAIT "Please enter your date of birth: "

because on nine runs out of ten the string would never
arrive and the script would be left 'hanging'. Using
WAIT/5, say, to time out the WAIT if the string were not
received, might work but imposes an unnecessary delay in
script processing which would be tedious in usage.

The best solution is to issue a WAIT or SENDWAIT command
to detect the next string which you *know* is going to
arrive and, before it, issue a WHEN command to detect the
date of birth prompt, if it arrives.

e.g.,

While not connected
   Dial "number"
EndWhile
When "Please enter your date of birth:" 120145^M
Wait "Main Menu"
SendWait m^M "Message Menu"
EndWhen

Note that searching for long strings is wasteful of
processing time. It is better to search for shorter
strings, as long as tbey are guaranteed to be unique and
therefore not to be confused with any other strings which
might be received.

e.g., The When command above would probably be better
written as

When "date of birth:" 120145^M

or even

When "birth:" 120145^M

Note also that Scripta always waits until the remote
computer is quiet before responding to WHEN commands, so

When "date of"

would probably work just as well.

When a script is running, you may press F3 at any time to
display all currently outstanding WHENs, as well as the
'wait' strings of any currently active command which
includes a WAIT element (F3 also displays the command
currently being processed). This facility is particularly
useful when testing new scripts. If the remote system goes
quiet, but you stay connected, then the chances are that
both the remote and your own computer are each waiting for
the other to send something. If you press F3, you can at
least see what your own end is expecting. Press F2 to
abandon the script and continue manually. Later, you can
press F2 again if you wish, to return to execution of the
script. It is however very rare that return to an
interrupted script serves any useful purpose.
