In This Chapter
- WSH: Your Host for Today's Script
- Scripts and Script Execution
- Programming Objects
- Programming the WScript Object
- Programming the WshShell Object
- Programming the WshNetwork Object
- Example: Scripting Internet Explorer
In Appendix C, "Automating Windows XP with Batch Files," you learn how to tame the command prompt by creating batch files—small, executable text files that run one or more commands. You'll see that with a little ingenuity and a dash of guile, it's possible to make batch files perform some interesting and useful tasks. Indeed, for many years, batch files were the only way to automate certain kinds of tasks. Unfortunately, the batch file world is relentlessly command-line–oriented. So, with the exception of being able to launch Windows programs, batch files remain ignorant of the larger Windows universe.
If you're looking to automate a wider variety of tasks in Windows, you need to supplement your batch file knowledge with scripts that can deal with the Registry, shortcuts, files, and network drives, and that can even interact with Windows programs via Automation. The secret to these powerful scripts is the Windows Script Host (WSH). This chapter introduces you to the Windows Script Host, shows you how to execute scripts, and runs through the various elements in the Windows Script Host object model.
WSH: Your Host for Today's Script
The key difference between Internet Explorer's script hosting and the Windows Script Host is the environment in which the scripts run. Internet Explorer scripts are web page–based, so they control and interact with either the web page or the web browser. The Windows Script Host runs scripts within the Windows Vista shell or from the command prompt, so you use these scripts to control various aspects of Windows. Here's a sampling of the things you can do:
- Execute Windows programs
- Create and modify shortcuts
- Use Automation to connect and interact with Automation-enabled applications such as Microsoft Word, Outlook, and Internet Explorer
- Read, add, and delete Registry keys and items
- Use pop-up dialog boxes to display information to the user, and determine which button the user clicked to dismiss the dialog box
- Read environment variables, which are system values that Vista keeps in memory, such as the folder into which Vista is installed—the %SystemRoot% environment variable—and the name of the computer—the %ComputerName% environment variable
- Deal with network resources, including mapping and unmapping network drives, accessing user data (such as the username and user domain), and connecting and disconnecting network printers
Clearly, we've gone way beyond batch files!
What about speed? After all, you wouldn't want to load something that's the size of Internet Explorer each time you need to run a simple script. That's not a problem because, as I've said, the Windows Script Host does nothing but host scripting engines, so it has much less memory overhead than Internet Explorer. That means that your scripts run quickly. For power users looking for a Windows-based batch language, the Windows Script Host is a welcome tool.