Home > Articles > Home & Office Computing > Microsoft Windows Vista & Home Server

Windows 7 Scripting and Objects

In this chapter Brian Knittel introduces the concepts of objects, methods, and properties in Windows 7.
This chapter is from the book

Introduction to Objects

All the Windows Scripting languages that I discussed in Chapter 1, "Windows Script Host," provide the basic tools to control a script's execution and to manipulate strings, numbers, dates, and so on, but they don't necessarily provide a way of interacting with Windows, files, or application software. These functions are provided by objects—add-on components that extend a programming language's intrinsic capabilities. In this section, I discuss what objects are and introduce the terms you run into as you work with them. In the following sections, I discuss how objects are actually used in several programming languages.

In the most general sense, objects are little program packages that manipulate and communicate information. They're a software representation of something tangible, such as a file, a folder, a network connection, an email message, or an Excel document. Objects have properties and methods. Properties are data values that describe the attributes of the thing the object represents. Methods are actions—program subroutines—you can use to alter or manipulate whatever the object represents.

For example, a file on your hard disk has a size, creation date, and name. So, these are some of the properties you would expect a File object to have. You can rename, delete, read, and write a file, so a File object should provide methods to perform these tasks. An important aspect of objects is that they are self-contained and separate from the program that uses them. How the object stores and manipulates its data internally is its own business. The object's author chooses which data and procedures to make accessible to the outside world. In programming jargon, we say that an object exposes properties and methods; these items compose its interface. Figure 3.1 shows the interface of a hypothetical File object.

Figure 3.1

Figure 3.1 This hypothetical File object has an interface that can be used by other programs. How the File object actually stores its information and does its job are hidden.

Objects need a mechanism through which they can exchange property and method information with a program or script. Because each programming language has a unique way of storing and transferring data, objects and programs must use some agreed-upon, common way of exchanging data. For scripting and for most Windows applications, Microsoft uses what it calls the Component Object Model (COM). COM objects can be used by any compatible language, including VBScript, JScript, C, C++, C#, Visual Basic, Perl, and so on. COM objects can also be called ActiveX Objects, Automation Objects, or OLE objects, if they have certain additional features, but regardless of what they're called, the technology is based on COM.

In the next several chapters, you see objects that represent files, folders, network connections, user accounts, printers, Registry entries, Windows applications, email messages, and many more aspects of your computer and network. Windows comes with software to provide you with a wealth of objects. You can also download, buy, or create additional objects of your own devising.

Classes and Instances

Two other terms you're likely to run into while working with objects are class and instance. The distinction is the same as that between a blueprint for a house and the house itself.

The word class refers to the object's definition: its interface (the properties and methods it provides) and its implementation (the hidden programming inside that does the actual work). Hundreds of useful object classes are provided with Windows, and you can add or create more, as discussed in Appendix G, "Creating Your Own Scriptable Objects," which you can download at www.helpwin7.com/scripting.

When you use an object in a program, the class program creates one or more instances of the object. An instance is a parcel of computer memory set aside to hold the object's data. The class program then gives your program a reference to use when manipulating the object—some identifying value that the class program can use to determine which particular instance of the object your script or program is using. Figure 3.2 illustrates this point: Variables file1 and file2 are variables that reference two instances of a File object.

Figure 3.2

Figure 3.2 File1 and File2 refer to objects of the File class. This illustration shows two instances of the File object.

A reference is treated like any other variable in your program. Just as you can use functions such as sqrt() and left() to manipulate numeric and string values, you can use the object's methods and properties to manipulate an object reference.

Containers and Collections

As mentioned earlier, an object represents some real-world, tangible thing, such as a document or hard drive, and it has properties that represent the tangible thing's attributes. For example, an apple object might have attributes such as color and tartness. The actual data stored for the color might be a character string such as "red" or "green". Tartness might be represented as number from 0 (sugary sweet) to 10 (brings tears to your eyes).

An object describing a file on a hard drive might have properties such as name (a character string) and size (a number). An object representing a hard drive might have properties describing the hard drive's size, volume name, and also the drive's contents.

Now, the contents of a hard drive could be represented as a list of filenames or an array of string values. However, it might be more useful if the hard drive could yield a list of file objects that you could then use to work with the files themselves. This is actually how many objects work. When appropriate, objects can return references to other objects. When an object needs to give you several other objects, it will give you a special object called a collection, which holds within it an arbitrary number of other objects. For example, a Folder object might represent a folder on your hard drive, and its Files property might yield a collection of File objects, which represent each of the files in the folder, as illustrated in Figure 3.3.

Figure 3.3

Figure 3.3 File and Folder objects can represent the contents of a hard drive. The contents of a folder can be represented by collections of File and Folder objects.

A collection object can actually hold any type of object inside it, and the collection object itself has properties and methods that let you count, extract, and work with these objects. This is a common thing to see in object programming: "container" objects that contain other objects of any arbitrary type.

Windows ActiveX objects use container objects that have two properties: Item and Length. The Length property indicates how many items are in the collection. The Item property retrieves one of the individual items. For some collections, you can extract individual objects from the Item collection using Item(0), Item(1), and so on. For many collections, though, the Item property requires a name or other arcane bit of identifying information. Therefore, each scripting language provides a more general way of letting you examine all the objects in a collection. I discuss this in more detail later in the chapter.

Collections are pervasive in Windows script programming, and some languages have special means of working with them. I give examples of using collections in each of the scripting languages discussed later in the chapter.

Object Naming

Because objects are separate program components, scripts and other programs that use them need a way to locate them and tell Windows to activate them. In this section, I describe how this is done.

Each programmer who creates an object class gives it a name that, with any luck, is fairly self-explanatory. For example, Scripting.FileSystemObject is designed to be used by Windows Script Host (WSH) programs to view and manage hard drives, files, and folders. Each of the programming languages you can use with WSH has a way of creating an object instance given just this name. For example, in VBScript, the statement

   set fsobj = CreateObject("Scripting.FileSystemObject")

does the job, whereas in Open Object REXX, the comparable statement is

   fsobj  = .OLEObject~New("Scripting.FileSystemObject")

In either case, the statement causes the WSH interpreter to ask Windows to create an instance of the specified object. Windows looks up the object name in the Registry, finds the name of the program file that manages this object class (usually a file whose name ends in .dll or .ocx), and fires up the add-on program. The class program creates an instance of the object and gives your script a reference with which it can use and manipulate the object.

I show you how to do this in each WSH-compatible language later in this chapter. For almost all cases, this is all you need.

In the remainder of this chapter, I tell you how to use objects in VBScript and other languages. The next section on VBScript follows the tutorial style of Chapter 2, "VBScript Tutorial," whereas the descriptions for other languages assume more experience with programming.

Finally, at the end of the chapter, I tell you how to find useful objects not discussed in the later chapters of this book.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020