Communicating with applications

In this section we will discuss how WSH scripts can communicate with other Windows applications. We will use only two methods to do that. Both of these are the methods of the WshSHell object. These two methods are:
  • AppActivate()
  • SendKeys()

    The methods AppActivate() activates an application window. It takes only one argument, which specifies which application to activate. This can be a string containing the title of the application (as it appears in the title bar) or the application's Process ID.

    The AppActivate method returns a Boolean value that identifies whether the procedure call is successful. This method changes the focus to the named application or window, but it does not affect whether it is maximized or minimized. Focus moves from the activated application window when the user takes action to change the focus (or closes the window).

    In determining which application to activate, the specified title is compared to the title string of each running application. If no exact match exists, any application whose title string begins with title is activated. If an application still cannot be found, any application whose title string ends with title is activated. If more than one instance of the application named by title exists, one instance is arbitrarily activated. Thus, the following example is trying to activate the notepad application with loaded test.txt file

    var WSH_Shell = WScript.CreateObject("WScript.Shell");
    if( WSH_Shell.AppActivate("test.txt - Notepad") ){
       ...
    }
    

    The SendKeys() sends one or more keystrokes to the active window (as if typed on the keyboard). It takes only one argument - a string value indicating the keystroke(s) you want to send.

    Use the SendKeys method to send keystrokes to applications that have no automation interface. Most keyboard characters are represented by a single keystroke. Some keyboard characters are made up of combinations of keystrokes (CTRL+SHIFT+HOME, for example). To send a single keyboard character, send the character itself as the string argument. For example, to send the letter x, send the string argument "x".

    You can use SendKeys to send more than one keystroke at a time. To do this, create a compound string argument that represents a sequence of keystrokes by appending each keystroke in the sequence to the one before it. For example, to send the keystrokes a, b, and c, you would send the string argument "abc". The SendKeys method uses some characters as modifiers of characters (instead of using their face-values). This set of special characters consists of parentheses, brackets, braces, and the:
  • plus sign
  • "+",
  • caret
  • "^",
  • percent sign
  • "%",
  • and tilde
  • "~"
    Send these characters by enclosing them within braces "{}". For example, to send the plus sign, send the string argument "{+}". Brackets "[ ]" have no special meaning when used with SendKeys, but you must enclose them within braces to accommodate applications that do give them a special meaning (for dynamic data exchange (DDE) for example). Some keystrokes do not generate characters (such as ENTER and TAB). Some keystrokes represent actions (such as BACKSPACE and BREAK). To send these kinds of keystrokes, send the arguments shown in the following table:
    Key Argument
    BACKSPACE {BACKSPACE}, {BS}, or {BKSP}
    BREAK {BREAK}
    CAPS LOCK {CAPSLOCK}
    DEL or DELETE {DELETE} or {DEL}
    DOWN ARROW {DOWN}
    END {END}
    ENTER {ENTER} or ~
    ESC {ESC}
    HELP {HELP}
    HOME {HOME}
    INS or INSERT {INSERT} or {INS}
    LEFT ARROW {LEFT}
    NUM LOCK {NUMLOCK}
    PAGE DOWN {PGDN}
    PAGE UP {PGUP}
    PRINT SCREEN {PRTSC}
    RIGHT ARROW {RIGHT}
    SCROLL LOCK {SCROLLLOCK}
    Key Argument
    TAB {TAB}
    UP ARROW {UP}
    F1 {F1}
    F2 {F2}
    F3 {F3}
    F4 {F4}
    F5 {F5}
    F6 {F6}
    F7 {F7}
    F8 {F8}
    F9 {F9}
    F10 {F10}
    F11 {F11}
    F12 {F12}
    F13 {F13}
    F14 {F14}
    F15 {F15}
    F16 {F16}
    To send keyboard characters that are comprised of a regular keystroke in combination with a SHIFT, CTRL, or ALT, create a compound string argument that represents the keystroke combination. You do this by preceding the regular keystroke with one or more of the following special characters:
    Key Special Character
    SHIFT   +
    CTRL ^
    ALT %
    To specify that a combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, create a compound string argument with the modified keystrokes enclosed in parentheses. For example, to send the keystroke combination that specifies that the SHIFT key is held down while: You can use the SendKeys method to send a pattern of keystrokes that consists of a single keystroke pressed several times in a row. To do this, create a compound string argument that specifies the keystroke you want to repeat, followed by the number of times you want it repeated. You do this using a compound string argument of the form {keystroke number}. For example, to send the letter "x" ten times, you would send the string argument "{x 10}". Be sure to include a space between keystroke and number.