共用方式為


AppActivate Method

Activates an application window.

                      object.AppActivate title 

Arguments

  • object
    WshShell object.

  • title
    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.

Remarks

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.

Return Value

The AppActivate method returns a Boolean value that specifies whether the activation operation is successful. The method might return False under the following conditions:

  • The window is not brought to the foreground.

  • The window is brought to the foreground but is not given keyboard focus.

  • A Command Prompt window (cmd.exe) is brought to the foreground and is given keyboard focus.

Note

The method can return False when an internal call to SetForegroundWindow succeeds but an internal call to SetFocus fails. This is known to occur when the target window is attached to another thread's message queue, which can occur when a window is hosted by another process. An example is a Command Prompt window that is hosted by the Console Window Host process.

Example

Description

The following example demonstrates the use of a single .wsf file for two jobs in different script languages (VBScript and JScript). The functionality of both jobs is the same — each runs the Windows calculator and sends it keystrokes to execute a simple calculation.

The following example starts the Windows calculator and uses AppActivate to ensure that the calculator is at the top.

<package>
   <job id="vbs">
      <script language="VBScript">
         set WshShell = WScript.CreateObject("WScript.Shell")
         WshShell.Run "calc"
         WScript.Sleep 100
         WshShell.AppActivate "Calculator"
         WScript.Sleep 100
         WshShell.SendKeys "1{+}"
         WScript.Sleep 500
         WshShell.SendKeys "2"
         WScript.Sleep 500
         WshShell.SendKeys "~"
         WScript.Sleep 500
         WshShell.SendKeys "*3"
         WScript.Sleep 500
         WshShell.SendKeys "~"
         WScript.Sleep 2500
      </script>
   </job>

   <job id="js">
      <script language="JScript">
         var WshShell = WScript.CreateObject("WScript.Shell");
         WshShell.Run("calc");
         WScript.Sleep(100);
         WshShell.AppActivate("Calculator");
         WScript.Sleep(100);
         WshShell.SendKeys("1{+}");
         WScript.Sleep(500);
         WshShell.SendKeys("2");
         WScript.Sleep(500);
         WshShell.SendKeys("~");
         WScript.Sleep(500);
         WshShell.SendKeys("*3");
         WScript.Sleep(500);
         WshShell.SendKeys("~");
         WScript.Sleep(2500);
      </script>
   </job>
</package>

Applies To:

WshShell Object

See Also

Concepts

Running Your Scripts

Reference

SendKeys Method

Change History

Date

History

Reason

March 2011

Added information about the return value.

Information enhancement.