In this chapter, you’ll learn how to:
Create and execute scripts.
Program objects and use their properties and methods.
Display text to the user.
Run applications.
Read, add, and delete registry keys and values.
Control Internet Explorer via scripting.
Microsoft Windows XP comes with the welcome ability to run scripts that can deal with many aspects of the system, including drives, folders, files, and the registry; scripts can also interact with Windows programs via Automation. Scripts enable you to automate many routine tasks that would otherwise require tedious effort on your part. For example, you can enhance your system privacy by creating a script to automatically delete the items in your Recent Documents list when you shut down your computer. The power of these scripts lies in the Windows Script Host (WSH), an administration tool that allows you to use virtually any scripting language to exercise extensive control of most Windows components and many Windows-based applications. This chapter introduces you to the Windows Script Host, shows you how to execute scripts, and runs through the most useful elements in the Windows Script Host object model. This will help you understand and use the WSH scripts that appear throughout the rest of the book.
Scripts are simple text files that you create using Notepad or some other text editor. You can use a word processor such as WordPad to create scripts, but you must make sure that you save these files using the program’s "text only" document type.
When you first save the file, be sure to use the .vbs extension, which specifies the document as a VBScript script file. (We’re assuming you’ll be programming in VBScript, but WSH also supports other languages. If you program in a different language, you must name your files accordingly. For example, JavaScript script files must use the .js extension.)
As described in the next three sections, you have three ways to run your scripts: by launching the script files directly, by using Wscript.exe, or by using Cscript.exe.
We can only scratch the scripting surface in a single chapter. Fortunately, Microsoft has an extensive set of scripting tutorials and references on its MSDN Web site:
http://msdn.microsoft.com/nhp/default.asp?contentid=28001169
The easiest way to run a script from within Windows XP is to launch the .vbs file directly using any of the following techniques:
The .vbs file type has an "open" action that’s associated with WScript (Wscript.exe), which is the Windows-based front end for the Windows Script Host. In other words, launching a script file named, say, Myscript.vbs, is equivalent to entering the following command in the Run dialog box:
wscript myscript.vbs
The WScript host also defines several parameters that you can use to control how the script executes. Note that these parameters all start with a double-slash so as to ensure the command interpreter can distinguish these parameters from any command arguments that might start with a single slash. Here’s the full syntax:
WSCRIPT filename arguments //B //D //E:engine //H:host //I //Job:xxxx //S //T:ss //X
A script job is a section of code that performs a specific task or set of tasks. Most script files contain a single job. However, it’s possible to create a script file with multiple jobs. To do this, first surround the code for each job with the <script> and </script> tags, and then surround those with the <job> and </job> tags. In the <job> tag, include the id attribute and set it to a unique value that identifies the job. Finally, surround all the jobs with the <package> and </package> tags. Here’s an example:
<package> <job id="A"> <script language="VBScript"> WScript.Echo "This is Job A." </script> </job> <job id="B"> <script language="VBScript"> WScript.Echo "This is Job B." </script> </job> </package>
Save the file using the .wsf (Windows Script File) extension.
For example, the following command runs Myscript.vbs in batch mode with a 60-second maximum execution time.
wscript myscript.vbs //B //T:60
If you plan on writing a lot of scripts, the Microsoft Script Debugger is an excellent programming tool. If there’s a problem with a script, the debugger can help you pinpoint its location. For example, the debugger enables you to step through a script’s execution one line at a time. If you don’t have the Microsoft Script Debugger, you can download a copy from the following Microsoft site:
The Windows Script Host has a second host front-end application called CScript (Cscript.exe), which enables you to run scripts from the command line. At its simplest, you launch CScript and use the name of the script file (and its path, if required) as a parameter, as in this example:
cscript myscript.vbs
The Windows Script Host displays the following banner and then executes the script:
Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
As with WScript, the CScript host has an extensive set of parameters you can specify:
CSCRIPT filename arguments //B //D //E:engine //H:host //I //Job:xxxx //S //T:ss //X //U //LOGO //NOLOGO
This syntax is identical to that of WScript, but adds the following three parameters:
| Displays the Windows Script Host banner at startup. |
| Hides the Windows Script Host banner at startup. |
| Uses Unicode for redirected input/output from the console. |
It’s also possible to set some of these options by using the properties that are associated with each script file. To see these properties, right-click a script file and then select Properties. In the property sheet that appears, select the Script tab. Note that when you make changes to these properties, the Windows Script Host saves your settings in a new file that has the same name as the script file, except with the .wsh extension. (For example, if the script file is Myscript.vbs, the settings are stored in Myscript.wsh.) To use these settings when running the script, use either WScript or CScript and specify the name of the .wsh file:
wscript myscript.wsh
3.143.237.136