How to use the Windows Script Host to automate an application

Summary

The Microsoft Windows Script Host is a language-independent scripting host for 32-bit Windows operating system platforms. Windows Script Host is integrated into Microsoft Windows 98, Windows ME, Microsoft Windows 2000, Windows XP, Windows Server 2003, and Microsoft Internet Information Server 4.0. It is included with Microsoft Windows NT 4 Option Pack. To download the Windows Script Host, visit the following Microsoft Developer Network (MSDN) Web site:

More Information

This article describes how to use the Windows Script Host to run, activate, and send keystrokes to applications.

The Windows Script Host Object Model includes a versatile Run() method that allows programmers to specify window states of the applications they run. The AppActivate method allows programmers to activate applications by passing the target application's windows caption. The SendKeys method allows programmers to send keystrokes to the activated application. Use the following code:

*Run Winword, give it a string and sort it and then run Notepad and paste in.
#Define VFPCRchr(13)+chr(10)
*Constants for window state when using run method
#Define WSHIDE 0 && Hides the window and activates another window.
#Define WSNORMAL 1 && Activates and displays window.
#Define WSMIN 2 && Activates the window and displays it as a minimized window.
#Define WSMAX 3 && Activates the window and displays it as a maximized window.
#Define WSLAST 4 && Displays a window in its most recent size and position. The active window remains active.
#Define WSSAME 5 && Activates the window and displays it in its current size and position.
#Define WSMINNEXT 6 && Minimizes the specified window and activates the next top-level window in the Z order.
#Define WSMINSAME 7 && Displays the window as a minimized window. The active window remains active.
#Define WSSAMECUR 8 && Displays the window in its current state. The active window remains active.
#Define WSORIG 9 && Activates and displays the window.
#Define WSSETSHOW 10 && Sets the show state based on the state of the program that started the application.
Declare Sleep IN WIN32API INTEGER &&wait for milliseconds-may not be necessary in all instances
WshShell = CreateObject("wscript.Shell")
_Cliptext="One"+VFPCR+"Two"+VFPCR+"Three"+VFPCR+"Four"+VFPCR+"Five"+VFPCR
WshShell.Run( "WinWord", WSNORMAL) && Run normal
=Sleep(1000)
WshShell.SendKeys( "^v") &&Ctrl++v
=Sleep(100)
WshShell.SendKeys( "%as") &&Alt+a+s
=Sleep(100)
WshShell.SendKeys( "{ENTER}") &&Enter
=Sleep(100)
WshShell.SendKeys( "^a") &&ctrl+a
=Sleep(100)
WshShell.SendKeys( "^c") &&ctrl+c
=Sleep(100)
WshShell.SendKeys( "%{F4}") &&Alt+F4
=Sleep(100)
WshShell.SendKeys( "n") && n-(No)
=Sleep(100)
WshShell.Run("notepad",1)
=Sleep(100)
WshShell.AppActivate("notepad")
=Sleep(1000)
WshShell.SendKeys( "^v") &&Ctrl+V Sorted!

References

For additional information, visit the following MSDN Web site:

See the following documentation on this Web site:

White paper: Windows Script Host: A Universal Scripting Host for Scripting Languages

Technical Paper: Windows Script Host Programmer's Reference
Eigenschaften

Artikelnummer: 259103 – Letzte Überarbeitung: 20.06.2008 – Revision: 1

Feedback