OnAction...EndAction

Synchronization function.

The OnAction...EndAction function handles events. Not available in WinTask Lite. Its syntax is similar to Pause function.

See also

OnAction Error if you want to do specific actions whenever an error occurs at execution.

Syntax

OnAction <action_ident>
Button(<Button>,<Type>)
or
Key(<key_list>)
or
Menu(<menu_item>)
or
Text(<text>)
or
WinStatus(Exists|NotExists|Active|NotActive[,Exact|Near])
inWindow(<window_name>)
or
Date(<Date>)
Time(<Hour>)
or
Pause x seconds
DoSub <sub_name>
EndAction

Parameters

<action_ident>, name of the event to be handled. It is a constant unique for this event. This identifier can be used to deactivate this particular event.

An event can be a:

Button: <button>, name of the button to be pressed to activate the event.
Key: <key_list>, key(s) to be typed to activate the event ; the wizard list of keys can be used if you want to define a key combination, such as Alt+F1. Note that the PrintScreen key cannot be used to trigger a keyboard event.
Menu: <menu_item>, menu item to select in order to activate the event.
Text: <text>, text to be found in order to activate the event. The frequency with which text is checked is determined by the variable #TextLookFrequency.
Window: <window_name>, name of the window which activates the event.
Date: <Date>, date which will activate the event.
Time: <Hour>, time which will activate the event.

<sub_name>, subroutine to be executed as soon as the event occurs.

Remarks

Such an event can happen at any time. But its nature is known - keyboard event, mouse event, menu event, date/time event, duration event. All these events are external - if WinTask simulates the action the event is not fired.

For every event, an entry is created in a table of events. During execution, WinTask continually checks to see if one of the events listed in the table has occured.

If an event occurs during execution of the script, the procedure <sub_name> is immediately executed, then the script continues. This <sub_name> is a subroutine without any parameters.

This event handler mechanism is reentrant : during the execution of <sub_name>, if the same event or another event in the table occurs, <sub_name> is suspended and the new subroutine for this new event is launched. When the second event handler is complete, execution continues in the previous event handler.

Activation of the an event handler:
An event handler (and the execution of the associated subroutine) is activated as soon as the statement OnAction has been interpreted in the script. It stays active until the script ends. At the end of the script, all the events handled by that script are removed from the table of events, unless you use the SLEEP function (see the full example below). If a script launches another script (using the RUN statement), the event handler in the parent script remains active during execution of the child scripts (the table of events is shared by all the scripts currently running).
It is possible to deactivate the one event handler by the DISABLE function, but this function must be inserted in the script in which the event was defined (not in a parent/child script).

When an event handler is re-triggered:
For keyboard/mouse/menu events, each time the event occurs.
For text events, the text which has been handled must have disappeared before it can be triggered again.

Examples

Every time the user presses <Alt <F10>>, the script executes <name_sub> :

OnAction Active_Alt_F10

  Key("<Alt <F10>>")

  DoSub <name_sub>

EndAction

Script under Windows 98 which uses an OnAction on Window for reconnecting automatically every time the internet connection is lost.

 

Sub DoReconnect()

UseWindow("RNAAPP.EXE|#32770|Restablishing connection",1)

Click(Button,"Reconnect...")

EndSub

 

OnAction ReconnectReq

WinStatus(Exists, Exact)

InWindow("RNAAPP.EXE|#32770|Restablishing connection",1)

DoSub DoReconnect

EndAction

 

Sleep()

 

Every time the user clicks the right mouse button inside Word, the script runs <name_sub> :

OnAction Right_Mouse_Word

  Button(Right,Down)

  InModule("WINWORD.EXE")

  DoSub <name_sub>

EndAction

 

Every time the user selects Goto from the Edit menu of Word, the script runs <name_sub> :

OnAction Menu_Edit_Goto

  Menu("&Edit|&Goto... Ctrl+G")

  InWindow("WINWORD.EXE|OpusApp|Microsoft Word - TEST.DOC",1)

  DoSub <name_sub>

EndAction

Examples code

This example shows how an event is defined by the OnAction command; this event occurs when the user selects Close from the Explorer menu.

Sub close()

Disable(wait_for_close_menu)

res=msgbox("Would you like to close the message box? ",4,"EXAMPLE")

if res=6 then

' if res=6 WinTask closes Explorer and stops the script

CloseWindow("EXPLORER.EXE|ExploreWClass|My Documents",2)

stop

endif

Enable(wait_for_close_menu)

EndSub

 

OnAction wait_for_close_menu

Menu("File|&Close")

InWindow("EXPLORER.EXE|ExploreWClass|My Documents",2)

DoSub close

EndAction

 

shell("explorer")

' Definition of the event, each time the user selects the option Close in the Explorer menu File,

' the script runs the procedure close().

sleep()

'leaves the management of the random events active.

 

 

 

 

This script shows the use of OnAction for doing some actions when user

presses some keys.

The structure is :
OnAction touche (touche is the onaction identifier)

Key("<Alt <F2>>") 'the key will be Alt+F2

'DoSub appuie_touche (when the user press the specified key, Alt+F2, the subroutine appuie_touche is launched)

REMARK : The compiler needs that the subroutine appuie_touche has been defined BEFORE the OnAction using it

EndAction

 

'********************************************************************

 

'example

 

'Definition of the subroutine which will be launched when "<Alt <F2>>" will be pressed

sub appuie_touche()

'here you can do what you want

msgframe("Key displayed",1)

pause 1

removeframe(1)

endsub

'--------------------------------------

'Definition of the subroutine to stop the script if you press "<Alt <F1>>"

sub termine()

msgframe("Script ended",1)

pause 1

removeframe(1)

stop

endsub

'---------------------------------------

'OnAction definition for "<Alt <F2>>"

OnAction touche

Key("<Alt <F2>>")

DoSub appuie_touche

EndAction

'---------------------------------------

'OnAction definition for "<Alt <F1>>"

OnAction Fin

Key("<Alt <F1>>")

DoSub termine

EndAction

'----------------------------------------

 

'The OnAction is active until the end of the script

'If the script has a SLEEP instruction, all the OnAction stay active

 

'SLEEP

sleep()