Working with Application Windows
Returns window-handle of the active application window; window-handle can be used with the majority of "window" key words (see also win_pattern, WIN-HWND, WIN-CHILD-HWND).
Example:
WIN-SET-TITLE: "%GetForegroundWindow%" "new_title"
The Windows operating system allows you to send special control messages to a specified window. These messages are called Windows Messages. Different windows can handle different messages - consult API of the specified program to learn which messages can you send to it's windows. Special win32-function SendMessage is used to send Windows Messages. Here is the description of this function:
LRESULT SendMessage( HWND hWnd, // handle of destination window UINT Msg, // message to send WPARAM wParam, // first message parameter LPARAM lParam // second message parameter );
SendMessageA is a postfix version of SendMessage function, that you can use in nnCron. This means, that if you know the Window Handle of a specified window, you can send Windows Messages to that window right from your tasks. Note, please, that SendMessageA is a postfix word, thus the arguments are passed to it in reverse order:
<lParam> <wParam> <Msg> <hWnd> SendMessageA
Value, that was returned by SendMessageA stays on the stack. Delete this value if you are not planning to use it in your task.
Examples:
\ clicking 'play' button in WinAmp 0 40045 273 WIN-HWND SendMessageA DROP \ clicking 'next' button in WinAmp 0 40048 273 WIN-HWND SendMessageA DROP \ getting current WinAmp state (play, stop, pause) 104 0 1024 WIN-HWND SendMessageA IF ... ELSE ... THEN \ setting Miranda state to 'online' 0 171144 273 WIN-HWND SendMessageA DROP \ setting Miranda state to 'do not disturb' 0 171146 273 WIN-HWND SendMessageA DROP
WIN-CLICK: "win_pattern" "button_pattern"
"Clicks" on a specified button in a specified window.
Word WIN-CLICK: can be used with modifier ALL, which is placed before it.
WIN-CLOSE: "win_pattern"
Sends instruction WM_CLOSE to a specified window, i.e. emulates a single click with left mouse button on the "diagonal cross" in the upper right corner of the window.
Word WIN-CLOSE: can be used with modifier ALL, which is placed before the word.
WIN-HIDE: "win_pattern"
Hides a specified window. The program will still be running, but its window will no longer be seen on screen. In order to make the window reappear, use word WIN-SHOW:.
Word WIN-HIDE: can be used with modifier ALL, which is placed before the word.
WIN-SHOW: "win_pattern"
Causes the windows that has been hidden with WIN-HIDE: and windows of applications started with option SWHide to be displayed on the screen.
Word WIN-SHOW: can be used with modifier ALL, which is placed before the word.
WIN-TERMINATE: "win_pattern"
Terminates the process which created a specified window.
Word WIN-TERMINATE: can be used with modifier ALL, which is placed before the word.
WIN-ACTIVATE: "win_pattern"
Activates a specified window.
Example:
#( test_win_activate \ activates window of Notepad application, using \ a regular expression as a mask NoActive Action: WIN-ACTIVATE: "/.*notepad/i" )#
It should be noted that while nnCron is running, variable %ACTIVE-WINDOW% always contains the title of currently active window.
There also exists a postfix version of this word:
S" win_pattern" WIN-ACTIVATE
WIN-ACTIVE: "win_pattern"
Returns TRUE (-1) if a specified window is active
WIN-EXIST: "win_pattern"
Returns TRUE (-1) if a specified window exists.
There also exists a postfix version of this word:
S" pattern" WIN-EXIST?
See also Note.
WIN-MINIMIZE: "win_pattern"
WIN-MAXIMIZE: "win_pattern"
WIN-RESTORE: "win_pattern"
These words, respectively, minimize, maximize and restore a specified window..
Words WIN-MINIMIZE:, WIN-MAXIMIZE: and WIN-RESTORE: can be used with modifier ALL, which is placed before them.
There also exists a postfix versions of these words:
S" pattern" WIN-MINIMIZE S" pattern" WIN-MAXIMIZE S" pattern" WIN-RESTORE
<win-hwnd> WIN-POS ( h -- x y )
A postfix word that takes as its argument window handle of a required window and returns two values which are coordinates of the upper left corner of this window.
Example:
#( test_position NoActive VARIABLE x VARIABLE y Action: WIN-EXIST: "nnCron: Options" IF BEEP: 250 500 WIN-HWND WIN-POS y ! x ! TMSG: "%FOUND-WINDOW% x=%x @% y=%y @%" 2 THEN )#
<win-hwnd> WIN-RECT ( h -- r b y x )
A postfix word that takes as its argument window handle of a required window and returns four values, which are the coordinates of the upper left and lower right corners of this window.
Example:
#( test_rect_position NoActive VARIABLE x VARIABLE y VARIABLE b VARIABLE r Action: WIN-EXIST: "nnCron: Options" IF BEEP: 250 500 WIN-HWND WIN-RECT x ! y ! b ! r ! TMSG: "%FOUND-WINDOW% x=%x @% y=%y @% b=%b @% r=%r @%" 2 THEN )#
A postfix word which resizes the active window. It uses height and width of this window (in pixels) as its arguments.
Examples:
600 400 WIN-RESIZE 800 800 WIN-RESIZE
WIN-TOPMOST: "win_pattern"
Places a specified window above all other windows. A window to which word WIN-TOPMOST: has been applied will stay above all other windows even after it loses the focus. To restore window's "normal" properties, just close it and then open it again or use WIN-NOTOPMOST: word.
Example:
#( test_win_topmost \ by pressing hot keys "CTRL+ALT+t", \ the currently active window is placed \ above of the rest of windows. WatchHotKey: "^@t" Action: WIN-TOPMOST: "%ACTIVE-WINDOW%" )#
WIN-NOTOPMOST: "win_pattern"
Cancel effect of WIN-TOPMOST:. By using WIN-TOPMOST: and WIN-NOTOPMOST: together, you can hold a window above all other windows for a required period of time and then restore its "normal" properties.
Example:
#( test_win_notopmost \ placing the specified window on top of the rest of windows, \ performing required action and canceling the effect of "WIN-TOPMOST:" NoActive Action: WIN-TOPMOST: "*Notepad" \ ... performing required operations WIN-NOTOPMOST: "*Notepad" )#
WIN-SEND-KEYS: "win_pattern" "key_code_string"
Activates a specified window and sends to it a sequence of key codes.
Example:
#( test_win_send_keys \ starting Notepad, making its window active \ and typing word "test" NoActive Action: START-APP: notepad.exe PAUSE: 1000 WIN-SEND-KEYS: "/.*notepad/i" "test" )#
Sometimes it becomes necessary to specify key codes for keyboard shortcut consisting of two or more keys that should be pressed simultaneously (e.g. CTRL is pressed and then, while it is being held down, ALT and G are pressed too). In this case, you should first indicated the first key that is being pressed and held down, and and state the remaining key codes after it in parentheses.
Examples:
\ "CTRL+s" WIN-SEND-KEYS: "window name" "^(s)" \ "CTRL+SHIFT+a" WIN-SEND-KEYS: "window name" "^(+a)" \ "SHIFT+F12" WIN-SEND-KEYS: "window name" "+({F12})"
Pauses between individual "keystrokes" and SEND-KEYS: constructs can be specified by using word SEND-KEYS-DELAY:.
See also Keyboard Input Emulation.
WIN-SET-TITLE: "win_pattern" "new_window_title"
Changes a title of a specified window. This word is frequently used to create unique titles for each of several simultaneously running instances of the same program .
Example:
#( test_set_title NoActive Action: START-APP: notepad.exe PAUSE: 500 WIN-SET-TITLE: "%GetForegroundWindow%" "first instance of Notepad " START-APP: notepad.exe PAUSE: 500 WIN-SET-TITLE: "%GetForegroundWindow%" "second instance of Notepad " )#
There also exists a postfix version of this word:
S" win_pattern" S" new_window_title" WIN-SET-TITLE
Moves the active window to a new position with absolute coordinates x and y (in pixels).
You can use WinSpy utility to find out the required coordinates.
Examples:
\ activating Notepad"s window and moving it to location \ with coordinates 100, 100 WIN-ACTIVATE: "*Notepad" WIN-MOVE: 100 100 \ and now moving it to the upper left corner of the screen WIN-MOVE: 0 0
Moves the active window to the specified amount of pixels from the current position.
Arguments of word WIN-MOVER: can take both positive and negative values:
You can use WinSpy utility to find out the required coordinates.
Examples:
\ activating Notepad"s window and moving it \ 100 pixels to the right and 100 pixels down WIN-ACTIVATE: "*Notepad" WIN-MOVER: 100 100 \ moving the window 250 pixels to the right \ and 100 pixels up WIN-MOVER: 250 -100 \ moving the window 250 pixels to the right WIN-MOVER: 250 0
Specifies the time during which nnCron will wait for the active window to get ready.
For example, if an application performs after startup some lengthy operation and its window does not react to user's actions, you can specify the time (in milliseconds) during which nnCron will wait for the active window to become ready.
If the active window becomes free before the specified time has expired, nnCron will immediately execute the next line of the task. Otherwise, it will execute it only after the entire period of waiting time has expired.
Example:
\ starting a slowly-starting program START-APP: "xxx.exe" PAUSE: 1000 \ waiting for 30 seconds for it to get ready WIN-WAIT: 30000 \ ... performing some operations with the window
FOR-WINDOWS: "win_pattern" <...> ;FOR-WINDOWS
Provides possibility to perform operations not on just a single window, but entire groups of windows matching a certain mask: commands specified in the body of "FOR-WINDOWS:" loop will be applied to each of these windows. Actually, definitions of all the "WIN*" words are based on this loop.
It works in this way: if there are five files matching a certain mask, then FOR-WINDOWS: loop will be automatically executed five times and each time value of variable %FOUND-WINDOW% will be renewed. (Variable %WIN-TITLE% can be used instead of %FOUND-WINDOW%, they both refer to the same thing). Value of variable WIN-HWND, which contains window handle of the currently processed window, is also renewed with each loop.
Example:
#( test_for_windows \ closing all found windows of Internet Explorer NoActive Action: FOR-WINDOWS: "*Internet Explorer" PAUSE: 300 WIN-CLOSE: "%FOUND-WINDOW%" ;FOR-WINDOWS )#
Therefore, with each iteration of the loop you get access to title of the next window matching the mask. Now it only remains to perform the required action with this window (close, move or minimize it etc.), or to perform a new check in order to decide what to do with this particular window.
Possibility to perform an additional check before performing actions with a window is the main difference between word FOR-WINDOWS: and modifier ALL.
Example:
#( test_for_windows1 \ displaying a query message box and closing \ all found windows of Internet Explorer NoActive Action: FOR-WINDOWS: "*Internet Explorer" QUERY: "Close window %FOUND-WINDOW%?" IF WIN-CLOSE: "%FOUND-WINDOW%" THEN ;FOR-WINDOWS )#
FOR-CHILD-WINDOWS: "win_pattern" <...> ;FOR-CHILD-WINDOWS
Provides possibility to perform operations on groups of child windows whose title matches a specified mask. Before you can use FOR-CHILD-WINDOWS:, variable WIN-HWND must contain the window handle of the window whose child windows we are going to work with.
Most of "WIN*" words, word WIN-EXIST: and loop FOR-WINDOWS:, when used, cause this variable to be set correctly.
FOR-CHILD-WINDOWS: loop sets its own variable WIN-CHILD-HWND which contains a window handle of a child window.
Examples:
\ after word "WIN-EXIST:", variable "WIN-HWND" \ is assigned a proper value WIN-EXIST: "xxx" IF FOR-CHILD-WINDOWS: "yyy" \ ... performing some operations on child windows ;FOR-CHILD-WINDOWS THEN \ in each "FOR-WINDOWS:" loop, variable "WIN-HWND" \ contains current window's window handle FOR-WINDOWS: "xxx" \ ... performing operations FOR-CHILD-WINDOWS: "yyy" \ ... performing some operations on child windows ;FOR-CHILD-WINDOWS ;FOR-WINDOWS
#( test_for_child_windows \ outputting to a log file names of all \ child windows of program"Xnews" NoActive Action: \ using word "WIN-EXIST:" to properly \ set the value of variable "WIN-HWND" WIN-EXIST: "*Xnews*" IF \ Using the class of child windows \ as argument of " FOR-CHILD-WINDOWS:" \ You can look up the class in "WinSpy" utility FOR-CHILD-WINDOWS: "TGroupsWindow" LOG: "log\nncron.log" "%FOUND-CHILD-WINDOW%" ;FOR-CHILD-WINDOWS THEN )#
Nearly all "WIN*" key words can be preceded by modifier ALL.
When this modifier is used, the command will be executed for all windows whose titles match a specified mask.
Examples:
\ minimizing all open windows of "Internet Explorer" ALL WIN-MINIMIZE: "*Internet Explorer" \ closing all found windows of "Command Prompt" ALL WIN-CLOSE: "/(Command Prompt)|(.*?cmd.exe)/i"
Note: after most of "WIN*" key words (as well as WIN-EXIST:), variable WIN-HWND will contain the window handle of corresponding window, and variable %FOUND-WINDOW% - will contain this window's title. (%WIN-TITLE% is a synonym for %FOUND-WINDOW%).
#( task-move-window \ displaying a query message box \ and moving the specified window to the desired screen location Action: WIN-EXIST: "*Internet*" IF QUERY: "Move "%FOUND-WINDOW%" to (0,0)?" IF WIN-ACTIVATE: "*Internet*" PAUSE: 100 WIN-MOVE: 0 0 THEN THEN )#
win_pattern is a mask of a window title or window class. Mask of a window title may include wildcard characters * and ? or regular expressions enclosed in slashes: /<regexp>/. Window handle of a required window (either in decimal or in hexadecimal form) can also be used in win_pattern. WinSpy utility can be used to find out the class, exact title and window handle of a required window.
Example:
\ window title mask WIN-HIDE: "Calculator" \ regular expression (based on a window title) WIN-HIDE: "/calculator/i" \ window class WIN-HIDE: "SciCalc" \ window handle (decimal form) WIN-HIDE: "592738" \ window handle (hexadecimal form) WIN-HIDE: "0x90B62"
See also: