[Previous] [Table of Contents] [Next]

Activating an Application Using the AppActivate Method

You can use the Run method of the WshShell object as shown earlier to launch external applications, and in WSH 2 you can switch an application to the foreground (activate the application) by using the AppActivate method.

Let's look at a simple scenario in which a script launches two applications. One application window gets the focus. If the user clicks on the other window, it receives the focus. To control a window from a WSH script (to send data to it with SendKeys, for example), you must ensure that the window keeps the focus.

The AppActivate method uses the following syntax:

obj.AppActivate title

The object variable obj contains a reference to the WshShell object, and title contains the title text shown in the window of the (already running) process that is to be activated. The short script in Listing 13-2 uses the AppActivate method. The script launches Calculator and Notepad and then changes the focus from one window to the next. The script asks the user for a window title and transfers the focus to the window containing the given title text.

Listing 13-2 AppActivateWSH2.vbs

' File:   AppActivateWSH2.vbs (WSH 2 sample in VBScript)   
' Author: (c) G. Born
' Launching Calculator and Notepad and using 
' AppActivate to switch between applications
Option Explicit

' Define the title strings of the application windows.
' Important: Strings depend on the localized version of Windows.
Const Edit_Title = "Untitled - Notepad"  ' Window title
Const Calc_Title = "Calculator"          ' Window title

Dim Wsh, win_title

' Create the WshShell object, which Run and AppActivate require.
Set Wsh = WScript.CreateObject("WScript.Shell")

' Try to launch two applications. To ensure that the last
' application receives the focus, delay the script.
Wsh.Run "Calc.exe", 1    ' Launch Calculator.
WScript.Sleep 800        ' Delay allows Calculator to get the focus.
Wsh.Run "Notepad.exe", 1 ' Launch Notepad.
WScript.Sleep 800        ' Delay allows Notepad to get the focus.

WScript.Echo "Click OK to set focus to the Calculator window"

' Set the focus back to the Calculator window.
Wsh.AppActivate Calc_Title 

' Set the focus to the Notepad window.
WScript.Echo "Click OK to set the focus to Notepad"
Wsh.AppActivate Edit_Title

' Ask user for a window title and set the focus to that window.
win_title = InputBox ("Please enter the window title", _
                      "Ask for window title", Calc_Title)
Wsh.AppActivate win_title

'*** End

Let's take a closer look at some of the code in Listing 13-2. First, you define two constants that hold the title strings of the windows:

Const Edit_Title = "Untitled - Notepad"  ' Window title
Const Calc_Title = "Calculator"          ' Window title

Defining these title strings as constants is handy because both Calculator and Notepad use localized titles. For example, German Windows uses different strings than U.S. Windows does. To adapt this sample to a localized version of Windows, you need only change the title text in the constants. Then you create a reference to WScript.Shell, which you need to execute the Run method:

Set Wsh = WScript.CreateObject("WScript.Shell")

After these preliminaries, you're ready to launch the applications by using the Run method:

Wsh.Run "Calc.exe", 1     ' Launch Calculator.
Wsh.Run "Notepad.exe", 1  ' Launch Notepad.

To avoid having the Calculator window get the focus because of a delay during loading, you use the Sleep method:

Wsh.Run "Calc.exe", 1     ' Launch Calculator.
WScript.Sleep 800         ' Delay allows Calculator to get the focus.
Wsh.Run "Notepad.exe", 1  ' Launch Notepad.
WScript.Sleep 800         ' Delay allows Notepad to get the focus.

A delay of 800 milliseconds is sufficient on my system to ensure that the Notepad window gets the focus. The second delay ensures that the script's dialog box is shown in the foreground after both applications are launched, as shown in Figure 13-2. If you omit the delay, one of the applications will probably become visible after the dialog box is shown. In the sample in Listing 13-2, the script's dialog box is hidden in the background and the application window receives the focus.

Click to view at full size.

Figure 13-2 Desktop with application windows and the script's dialog box, which keeps the focus

The following command transfers the focus to the Calculator window if the user clicks the OK button:

Wsh.AppActivate Calc_Title

Other dialog boxes the script invokes allow the user to inspect the results of each step. In the last step, the user can enter the title text of any window shown on the Desktop or on the taskbar. The script transfers the focus to this window.

The AppActivate method doesn't affect the window style when it changes the focus to a specified application or window—for example, it doesn't affect whether the window is maximized or minimized. Therefore, a button on the taskbar could receive the focus. WSH 2 doesn't provide a method for changing the window style.

Pitfalls of Using the AppActivate Method

To determine which application to activate, AppActivate compares the title parameter submitted to the method with the title string of each running application. If no exact match exists, any application whose title string begins with the pattern contained in the title variable is activated. If no application is found, any application whose title string ends with the pattern contained in the title variable is activated.

Although generally useful, the AppActivate method has several drawbacks:

The program shown in Listing 13-3 uses AppActivate in JScript and invokes two instances of Calculator. It then tries to switch the focus between the Calculator windows. Because both windows use the same title, no change occurs.

Listing 13-3 AppActivateWSH2.js

// File:     AppActivateWSH2.js (WSH 2 sample in JScript)   
// Author:   (c) G. Born
// Launching Calculator twice and trying to switch 
// between the windows

// Define the title strings of the application windows.
// Important: Strings depend on the localized version of Windows.
var Calc_Title = "Calculator";           // Window title

// Create the WshShell object, which Run and
// AppActivate require.
var Wsh = WScript.CreateObject("WScript.Shell");

// Try to launch two applications. To ensure that the
// last application receives the focus, delay the script.
Wsh.Run("Calc.exe", 1);     // Launch Calculator.
Wsh.Run("Calc.exe", 1);     // Launch second instance of Calculator.
WScript.Sleep(800);         // Delay until Calculator gets the focus.

// Switch focus between Calculator windows.
WScript.Echo("Click OK to set focus to Calculator window");

WScript.Echo("Click OK to set focus to second Calculator window");

//*** End

To summarize, the WSH 2 AppActivate method doesn't provide any way to change the window style, and the window title isn't the best means to identify a window. The WSH 2 Programmer's Reference says that AppActivate can use the process identifier obtained from the Shell function to activate a window, but WSH 2 doesn't support a Shell function.