Home > Articles > Operating Systems

  • Print
  • + Share This
  • 💬 Discuss
This chapter is from the book

Using Objects with JScript

JScript, like VBScript, is a strongly object-oriented language—it's expected that programmers will use objects to extend its power. JScript supplies 11 built-in object types, and programmers can create generic and structured object types in scripts. I won't discuss the intrinsic object types here because this book focuses on external scripting and Windows management objects.

External COM/ActiveX objects are created using the new statement, as follows:

variablename = new ActiveXObject("objectname");

Here, variablename is the declared variable that is to receive the new object reference.

Once you have an object variable in hand, its methods and properties are accessed using variable.propertyname or variable.methodname. For example, this is a short script that tells whether your C: drive has a folder named \windows:

var fso;
fso = new ActiveXObject("Scripting.FileSystemObject");
if fso.FolderExists("c:\\windows")
  WScript.echo("There is a folder named \\Windows");

Parentheses must be used on all method calls, even if the return value is not used. Whereas VBScript tolerates a statement such as

WScript.echo "There is a folder named \Windows"

JScript does not.

Case Sensitivity

In the Windows Script Host environment, JScript programs have access to a predefined object named WScript, which provides several useful methods and properties pertaining to the script's environment, execution, and debugging. Because JScript is a case-sensitive language, to use this object you must type WScript exactly as written here.

However, the method and property names of ActiveX and COM objects are not case sensitive. For example, JScript permits you to type WScript.echo or WScript.Echo.

Working with Collections

If you are used to using JScript with Internet Explorer in browser or server-side ASP scripts, you may be familiar with objects that return collections of objects. Many IE objects let you scan through the object collection using JScript's for...in statement.

However, most other objects' collections do not work with for...in and you must use an Enumerator object to work with them. This is true of most objects you'll encounter in the Windows Script Host environment. JScript's Enumerator object gives you a way of accessing a collection by stepping forward or backward through the collection's list of objects.

To use a collection provided by a scripting or other ActiveX object, you must first convert it to an enumerator:

enumObject = new Enumerator(collectionObject);

The Enumerator object has no properties (in particular, no Length property). It has an internal concept of its "position" in the collection and has methods that let you move the current position forward or back to the beginning. Its four methods are listed in Reference List 3.1.


Reference List 3.1 Methods of the JScript Enumerator Object.

Item

Returns the current item from the collection. The return value will be an object of whatever type the collection holds. If the collection is empty or the current position is undefined, it returns undefined.

AtEnd

Returns a Boolean value: True if the current item is the last in the collection, if the current position is undefined, or if the collection is empty. Otherwise, False is returned.

moveFirst

Makes the first item in the collection the current item. If the collection is empty, atEnd will immediately be True.

moveNext

Makes the next item in the collection the current item. If the collection is empty or the current item is already the last in the collection, the current item will be undefined.


Although a newly created enumerator should be positioned on the first item automatically, it's better style to use moveFirst before examining atEnd or item.


Pattern

To scan through a collection object named obj, use an enumerator in this way:

e = new Enumerator(obj);
for (e.moveFirst(); ! e.atEnd(); e.moveNext()) {
  x = e.item();
  ...statements using x
}

Here's an example. This script lists the names of all the files in the root folder on the C: drive:

var fso, e, file;

fso = new ActiveXObject("Scripting.FileSystemObject");

e = new Enumerator(fso.GetFolder("c:\\").files);
for (e.moveFirst(); ! e.atEnd(); e.moveNext()) {
  file = e.item();
  WScript.echo(file.name);
}

Now, if you plan on writing scripts only in JScript, you can skip ahead to the section titled "Using the WScript Object" for more information about the built-in WScript object.

  • + Share This
  • 🔖 Save To Your Account

Discussions

comments powered by Disqus