Scripting and Automation


Applications such as Microsoft Internet Explorer and Microsoft Word come with (expose, in the jargon) a set of objects that define various aspects of the program. For example, Internet Explorer has an Application object that represents the program as a whole. Similarly, Word has a Document object that represents a Word document. By using the properties and methods that come with these objects, it’s possible to programmatically query and manipulate the applications. With Internet Explorer, for example, you can use the Application object’s Navigate method to send the browser to a specified Web page. With Word, you can read a Document object’s Saved property to see whether the document has unsaved changes.

This is powerful stuff, but how do you get at the objects that are exposed by these applications? You do that by using a technology called Automation. Applications that support Automation implement object libraries that expose the application’s native objects to Automation-aware programming languages. Such applications are called Automation servers, and the applications that manipulate the server’s objects are called Automation controllers. The Windows Script Host is an Automation controller that enables you to write script code to control any server’s objects.

This means that you can use an application’s exposed objects more or less as you use the Windows Script Host objects. With just a minimum of preparation, your script code can refer to and work with the Internet Explorer Application object or the Microsoft Word Document object, or any of the hundreds of other objects exposed by the applications on your system. (Note, however, that not all applications expose objects. Outlook Express and most of the built-in Windows XP programs—such as WordPad and Paint—do not expose objects.)

The WScript object’s CreateObject method creates an Automation object (specifically, what programmers call an instance of the object). Here’s the syntax used with this method:

WScript.CreateObject(strProgID)

strProgID

A string that specifies the Automation server application and the type of object to create. This string is called a programmatic identifier, which is a label that uniquely specifies an application and one of its objects. The programmatic identifier always takes the following form:

AppName.ObjectType

Here, AppName is the Automation name of the application, and ObjectType is the object class type (as defined in the registry’s HKEY_CLASSES_ROOT key). For example, here’s the programmatic ID for Internet Explorer:

InternetExplorer.Application

Note that you normally use CreateObject within a Set statement, and that the function serves to create a new instance of the specified Automation object. For example, you could use the following statement to create a new instance of Internet Explorer’s Application object:

Set objIE = CreateObject(“InternetExplorer.Application”)

There’s nothing else you need to do to use the Automation object. With your variable declared and an instance of the object created, you can use that object’s properties and methods directly.




Insider Power Techniques for Microsoft Windows XP
Insider Power Techniques for Microsoft Windows XP (Bpg-Other)
ISBN: 0735618968
EAN: 2147483647
Year: 2005
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net