Introducing JScript

[ LiB ]

Introducing JScript

JScript is one of two default scripting languages supplied by Microsoft to use with the WSH. The other scripting language supplied with the WSH is VBScript. Functionally, JScript and VBScript are pretty much equivalent. Both are great scripting languages for automating small tasks .


VBScript is a scripting language based on a subset of the Visual Basic programming language. Like JavaScript,VBScript can also be used for Web page development. However, while Internet Explorer supports VBScript, it does not work with Netscape Communicator. This makes VBScript a less universally accepted scripting language. In addition, VBScript is proprietary.This means that Microsoft owns it, whereas the current version of JavaScript and JScript are based on the ECMAScript standards, so no one company controls its future. As a result, many people prefer JavaScript and JScript to VBScript.

JScript and JavaScript provide a better collection of built-in mathematical functions that assist in performing complex calculation. VBScript, on the other hand, provides better support for working with arrays. I'll talk more about functions and arrays on Saturday morning. The decision as to whether to use JScript or VBScript to automate Windows tasks is really just a matter of personal preference. People with a JavaScript background will be able to make the move to JScript with very little effort. Likewise, people with a strong Visual Basic programming background may prefer learning VBScript.


Several other third-party scripting languages are available that also can be used with the WSH.These languages include PERL, Python and REXX. A discussion of these scripting languages is beyond the scope of this book.To learn more about compatible versions of PERL and Python visit www. activestate .com.To learn more about REXX visit

The main reason for using JScript and the WSH is to save you time by automating system tasks. At the heart of every one of Microsoft's Windows operating systems is an intuitive graphical user interface (GUI). This GUI is designed to make the computer easy to use. All that you have to do is grab on to the mouse and point and click your way around.

Unfortunately, the GUI is not well suited to many tasks, particularly those that are highly repetitious or that involve many steps. For example, suppose that you are responsible for defining new user accounts on a shared computer in your department at work, and one day your boss comes in with a floppy disk containing a file with a list of 100 newly hired employees .

Using the GUI, you'd probably spend hours in front of the computer defining new user accounts, during which time no one else could use the computer to get any work done. On the other hand, you could write JScript and use the WSH to run it in less than an hour . This script could even be designed to read the file provided by your boss and to create a new user account for each name stored in the list. Once executed, the script would create the new user accounts within seconds. Best of all, once it is written, you could return and run the script again in the future with no additional work on your part to create new user accounts.


On Windows NT, XP, 2000, and 2003 operating system, Microsoft also supplies a built-in scripting language known as Windows shell scripting . Unlike the WSH, which supports script execution on all Windows operating systems (starting with Windows 95),Windows shell scripting is not supported by Windows 95, 98, and Me.While not as robust as the WSH,Windows shell scripting does provide a viable scripting option. If you want to learn more about Windows shell scripting, I recommend that you check out Windows Shell Script Programming for the Absolute Beginner , written by Jerry Lee Ford, Jr.You might also want to check out the Microsoft Windows Shell Scripting and the WSH Administra tor's Guide (Premier Press), also by Jerry Lee Ford, Jr.

Introducing the Windows Script Host

The WSH is an optional software component that supports the execution of scripts on Windows operating systems. It is tightly coupled with the operating system, meaning that it provides direct access to a number of Windows resources, including these:

  • The Windows desktop and Start menu

  • The Windows Quick Launch Bar

  • The Windows file system

  • Local and network printers and drives

  • Windows applications

  • Windows services

  • User accounts

  • The Windows registry

The WSH provides the most complete scripting environment available today for all Windows operating systems. When combined with a scripting language such as JScript, it provides a perfect platform for developing scripts that can automate virtually any Windows task.

What Operating Systems Are Compatible with the WSH?

So far Microsoft has released three versions of the WSH. WSH 1.0 was released initially as an add-on for Windows NT and 95. It was also supplied with Windows 98 when that operating was released. WSH 2.0 was introduced with Windows Me and 2000. The current version of the WSH is version 5.6, which was shipped with Windows XP and 2003.

Table 1.2 summarizes the versions of the WSH that were shipped with various Windows operating systems. As you can see, WSH does not support execution on Windows 3.X operating systems.


Operating System

Support for WSH

Version No.

Windows 3.X



Windows 95



Windows 98



Windows Me



Windows NT



Windows 2000



Windows XP



Windows 2003



Regardless of which of the Microsoft operating systems you own or support, you will want to make sure that you have upgraded the version of the WSH that is installed to the most current version. Microsoft provides access to the most current version of the WSH through its official scripting Web site,, where you can download WSH for free.

A Brief Overview of the WSH Architecture

The WSH provides built-in support for both JScript and VBScript. In addition, it can support any third-party, WSH-compatible scripting language, including PERL, Python, and REXX. In the future, third-party software developers may provide other WSH-compatible scripting languages. Microsoft designed the WSH to be very extensible, as evidenced by the number of third-party scripting languages that have already been set up to work with it. However, because JScript and VBScript are supplied with the WSH, they are the most commonly used scripting languages.

To work effectively with the WSH, you need to understand its architecture. Figure 1.7 provides a depiction of the different components that make up the WSH. As you can see, the WSH consists of scripting engines, execution hosts , and a core object model. In addition, it can work directly with object models supplied by other software applications.

Figure 1.7. The WSH is composed of a number of integrated components.



If you are comfortable developing scripts using more than one WSH-supported scripting language, the WSH enables you to create a special kind of script known as a Windows script file.These script files enable you to combine two or more scripts into a single script. This enables you to create scripts that take advantage of the strengths of different languages when automating tasks. For example, you might want to create a Windows script file that takes advantage of JavaScript's collection of mathematical functions and VBScript's strong support for creating and manipulating arrays. Since teaching you scripting languages other than JavaScript and JScript is outside the scope of this book, I won't be going into Windows script files any further. However, if you already know another scripting language, after you have completed reading this book you might want to visit to learn more about this WSH feature.

The WSH is a 32-bit application. Its architecture consists of three separate layers . First there are the various WSH-supported scripting engines. These scripting engines interpret script statements and pass them on to an execution host for processing.

The WSH provides two different execution hosts. The WScript execution host is designed to support the execution of scripts directly from the Windows desktop. The CScript execution host, on the other hand, is designed to support the execution of scripts from the Windows command prompt. Each of these execution hosts exists as a separate executable file named Wscript.exe and Cscript.exe , respectively.

With one exception, the WScript and CScript execution hosts provide the same level of functionality. This exception is the WScript execution host's capability to enable scripts to display text messages and collection input from users using graphical pop-up dialogs. This makes the WScript execution host the proper choice for scripts that need to interact directly with users. The CScript execution host, on the other hand, generally is used to run scripts that do not interact with users or to run scripts for users who are comfortable working from the Windows command prompt. However, the WScript execution host can be used to run scripts from the command line as well. Unless your script is designed to generate graphical pop-up dialogs, the choice of which script execution host to use is completely arbitrary.

The purpose of the WSH core object model is to provide access to Windows resources. It accomplishes this by defining objects that represent actual resources. These objects can then be accessed and manipulated by scripts, thus allowing the scripts to access and manipulate the resources that the objects represent. Examples of the types of objects that the WSH makes available include objects that represent Windows shortcuts, files, folders, and disk drives. The WSH also provides objects that represent network resources such as network drives and printers. On top of all this, the WSH object model also provides access to system resources such as Windows events logs and the Windows registry.

Every object that is presented by the WSH is associated with a collection of properties and methods . For example, a file is an example of an object. File objects have file names and file extensions. By accessing these file object properties, your JScripts can locate files that they want to work with. In addition, by modifying file object properties, your JScripts can rename files and change their file types.

The file object also provides JScripts with access to a number of different methods that enable you to create, delete, rename, copy, and move Windows files. Figure 1.7 provides a partial listing of the kinds of system resources exposed by the WSH core object model. On Sunday afternoon I'll cover the objects exposed by the WSH core object model and provide you with examples of how to access and manipulate them.

Writing Your First JScript

In order to create a JScript, you only need to open your text editor, create a plain text file, add a few JScript statements to it, and then save it with a .js file extension. For example, create a new JScript called Script 1.5.js and add the following statements to it.

 //Script 1.5 - Your first JScript WScript.Echo("Hello World!"); 

The first statement is a comment that was embedded within the file to document the script's name and purpose. The second statement displays a text message. The composition of the second statement is as follows . First the WScript object is referenced. This object is different from the WScript execution host. Then a method called Echo , which belongs to the WScript object, is executed and passed the text string "Hello World!" . Don't worry if this all sounds a little confusing or overwhelming now. By the time that you get through Sunday evening, you'll understand how this stuff works. For now, just follow along and focus on the steps involved in creating and executing your first JScript.

When executed by either the WScript or CScript execution host, this JScript displays its message. However, depending on which execution host you choose to use when running the script, the results will vary significantly. For example, if you were to run this script from the Windows command prompt using the CScript execution host, you'll see the output shown in Figure 1.8.

Figure 1.8. Running your first WSH JScript at the Windows command prompt



You may have noticed in Figure 1.8 that the name of the JScript file to be run was enclosed within a matching pair of double quotation marks. I had to do this because the script's file name contained a blank space. Otherwise the use of the double quotation marks would not have been required.

If you were to run the scripts directly from the Windows desktop by double-clicking on it, Windows would, by default, run it using the WScript execution host. As a result, the script's output would be displayed in the form of a graphical pop-up dialog, as shown in Figure 1.9.

Figure 1.9. Running your JScript from the Windows desktop



You can also use the WScript execution host to run scripts from the Windows command prompt,in which case the script will run exactly as if you had started it from the Windows desktop.

Getting Comfortable with the Windows Command Prompt

Even some of the most experienced computer users have limited experience working with the Windows command prompt and little if any understanding of the Windows commands that are available. However, if you plan on becoming a serious JScript programmer, you need to get comfortable working with the Windows shell.

The Windows shell provides a text-based interface between the user and the operating system. Instead of using the computer's mouse to point and click your way through the Windows graphical user interface, you work with the Windows shell by typing in text commands. The Windows shell translates these commands into a format the operating system can understand. Likewise, the Windows shell translates any output returned by the operating system into a format that people can understand.

You enter commands for the Windows shell to process by typing them in at the Windows command prompt. For example, on a computer running Windows XP, you would start up a new Windows shell session by clicking on Start, All Programs, Accessories, and then Command Prompt. The Command Prompt window appears and displays the Windows command prompt, as shown in Figure 1.10.

Figure 1.10. The Windows command prompt




As a shortcut, you can start up a new Windows shell session and access the Windows command prompt by clicking on Start, Run, typing CMD, and then clicking on OK.

As you can see in Figure 1.11, by default the Windows command prompt is represented in the form of a drive letter followed by a colon , a backslash, and the greater than character (>). Just to the right of the command prompt you will see a blinking cursor, which indicates that the Windows shell is ready to accept new command input. As an example, type VER and then hit the Enter key. The VER command instructs the Windows shell to display information about the version of Windows currently running on the computer, as demonstrated here.

Figure 1.11. Configuring the way the WScript execution host runs scripts that are started from the Windows desktop


 C:\>ver Microsoft Windows XP [Version 5.1.2600] C:\> 

The Windows shell passes the command to the operating system, collects the results of the command, and displays them in the back in the Windows console, after which the Windows command prompt is redisplayed.

Now, let's look at how to use the command prompt to run your new JScript using the CScript execution host. All that you have to do is type CScript, a space, and the name of your script and then press the Enter key as demonstrated below.

 CScript "Script 1.5.js" 

Likewise, to run your script from the command prompt using the WScript execution host, you would type the following.

 WScript "Script 1.5.js" 


If your script resides in a folder other than the folder upon which the Windows shell is currently focused, you must supply the complete path to the folder where the script is stored. For example, if your script were stored in C:\Jscripts , then you would need to use the following command to run it using the WScript execution host.

 WScript C:\Jscripts\"Script 1.5.vbs" 

When you are done running your script and want to close the Command Prompt Window, you can do so by clicking on Close in the upper right corner of the Windows. You can also close the Command Prompt window by right-clicking on the Command Prompt icon in the upper-left corner of the Window and selecting Close.



If you are faster at the keyboard than you are with your mouse then you can also close the Command Prompt windows by typing EXIT and pressing the Enter key.


A detailed discussion of the Windows shell is beyond the scope of this book. If you would like to learn more,check out the Microsoft Windows Shell Scripting and the WSH Admin istrator's Guide (Premier Press), written by Jerry Lee Ford, Jr.

Setting up WSH Execution Hosts

You can use the WScript and CScript execution hosts to run any JScript. The manner in which these execution hosts run scripts depends on how the execution hosts have been configured. Both the WScript and CScript execution hosts have their individual configuration settings. In addition, you can configure the WScript execution host to run differently when executed from the Windows command line and the Windows desktop.

In addition to enabling you to configure default execution host settings, the WSH also provides you with the capability to override these default settings when running individual scripts. This way you can temporarily change the execution settings that are in effect for a given script's execution without affecting the way the execution host runs other scripts.

Finally, if you have a few JScripts that require their own unique sets of configuration settings, you can store a customized set of configuration settings for each script in its own .wsh file. Each of these different execution host configuration options is further explored in the sections that follow.

Configuring Command-Line Execution

Both the WScript and CScript execution hosts support command-line execution. The syntax required to configure the execution setting for the CScript and WScript execution hosts is outlined below.

 CScript ScriptName [//Options] [Arguments] WScript ScriptName [//Options] [Arguments] 

ScriptName specifies the name of a script that is to be executed. //Options represents a collection of one or more optional parameters that configure a particular aspect of the execution host's operation. The Arguments attribute represents a list of arguments that can be passed to the script for processing.

The execution settings available for both execution hosts are identical. Table 1.3 briefly outlines these settings.





Displays the syntax requirements to use both the WScript and CScript commands.


Instructs the execution host to run the script in batch mode in order to suppress the display of errors and any other text output.


Turns on active debugging.

//e:jscript e:vbscript

Specifies the type of WSH script engine that should be used to interpret script statements and convert them to binary code.

//h:wscript h:cscript

Specifies which WSH execution host is to be used when executing the script.


Instructs the execution host to interactively execute the script in order to enable error and text output to be displayed.


Identifies a specific job within a Windows script file that is to be executed.


Tells the CScript or WScript execution host to display its logo before executing the script.


Tells the CScript or WScript execution host not to display its logo before executing the script.


Saves the specified execution settings, making them the default settings for future script executions.


Specifies the timeout value used to limit the script's maximum execution time. By default, no time limit is imposed on script execution.


Executes the script in debug mode.

Let's look at a few examples of different ways that the CScript and WScript execution hosts can be configured. For example, the following command can be used to tell the CScript execution host to run a script named Script 1.5.js without permanently modifying any of the execution host's configuration settings.

 CScript "Script 1.5.js" 

Likewise, you can run the same script using the WScript execution host using the following command.

 WScript "Script 1.5.js" 

Instead of running JScript using the execution host's default settings, you can temporarily override the execution host's configuration settings as demonstrated below.

 WScript "Script 1.5.js" //T:10 

In this example, the JScript is run using a 10-second execution time limit. By default, Windows operating systems set up the WScript execution host as the default WSH execution host. In other words, if you type the name of the script without specifying which WSH execution host to use, the WScript execution host will be used. To set the CScript execution host up as the default execution host, you could enter the following command.

 CScript //H:CScript 

Similarly, you can change the execution host back to WScript as shown below.

 WScript //H:WScript 

You can override more than one execution setting at a time by adding more options, each of which must be preceded by // characters as demonstrated below.

 WScript "Script 1.5.js" //T:10 //nologo 

In this example, the WScript execution host is used to run a script named Script 1.5.js with a 10-second time limit without first displaying the WScript execution host.

Each of the commands that we have looked at to this point affects only the execution for the specified script. To make the changes permanent, you'll need to add the //s option as demonstrated below.

 WScript //nologo //s 

This statement configures the WScript host so that it will not display its logo each time it runs a script from the Windows command prompt. Because you can configure the command-line execution of the WScript and CScript execution hosts separately, you can assign a completely different set of configurations to the CScript execution host.


Configuration changes made to the WSH's execution hosts are specific to the person who makes them.Therefore, any changes you make will not be in effect for other people who share the same computer you use.

Configuring WScript Desktop Execution

The WScript execution host's execution from the Windows desktop is configured separately from its command-line execution. When it is configured for Windows desktop execution, you can limit the amount of time that the script will be permitted to execute, as well as specify whether or not the WScript execution host's logo should be displayed. The following procedure outlines the steps involved in modifying the WScript execution host's desktop execution settings.

  1. Click on Start and then Run. The Run dialog box appears.

  2. Type WScript and then click on OK. The Windows Script Host Settings dialog box appears, as shown in Figure 1.11.

  3. By default no execution time limit is applied to script execution. To specify one, select Stop Script after Specified Number of Seconds and enter a number specifying the number of seconds within which scripts must finish executing.

  4. By default, the WScript logo is displayed when you run your JScripts. To prevent its display, clear the Display Logo when Script Executed in Command Console option.

  5. Click on OK.

Configuring Individual Script Execution

If you write a JScript to which you will never want to apply default execution host settings when you run it, you can create an individual Windows script host or .wsh file that specifies custom execution host settings for the script. The .wsh file must be assigned the same file name as the script with which it is associated but with a .wsh file extension. A script named Script 1.5.js would be named Windows script host file Script 1.5.wsh .

The following procedure outlines the steps involved in creating a Windows script host file for a given script.

  1. Right-click on your script and select Properties. The script's Properties dialog box appears.

  2. Select the Script property sheet, as shown in Figure 1.12.

    Figure 1.12. Configuring execution settings for individual scripts


  3. If desired, select the Stop Script after Specified Number of Seconds option and then specify the maximum number of seconds that the script should be permitted to execute.

  4. To prevent the display of the execution host's logo, clear the Display Logo when Script Executed in Command Console option.

  5. Click on OK.

Once you have completed the previous procedure, you should see a new file with the same file name and a .wsh file extension stored in the folder where your JScript is stored. Its contents will resemble this:

 [ScriptFile] Path=D:\Script 5.1.wsf [Options] Timeout=5 DisplayLogo=0 

[ScriptFile] is a header that identifies the file as a Windows script file. Path identifies the path and name of your JScript. [Options] is a header that identifies the start of a list of execution host configuration settings. Timeout sets the script's timeout settings. DisplayLogo specifies whether or not the execution host's logo should be displayed when the script is executed from the Windows command prompt. Set DisplayLogo equal to 0 to prevent the display of the execution host's logo and set it equal to 1 to enable its display.


If you prefer, you can create a script's Windows script host file manually. However, be careful when doing so to make sure that you don't make any typos when editing the file.

Once you have created a .wsh file for your JScript, the WSH will automatically refer to it the next time you run your script. However, if you delete the .wsh file or move your script to a different folder and forget to move its associated .wsh file also, the execution host will run the script using its default execution host configuration settings.

[ LiB ]

Learn JavaScript In a Weekend
Learn JavaScript In a Weekend, Second Edition
ISBN: 159200086X
EAN: 2147483647
Year: 2003
Pages: 84 © 2008-2017.
If you may any questions please contact us: