programming4us
         
 
 
Windows

Scripting Windows 7 with WSH : Programming the WScript Object

- Windows 10 Product Activation Keys Free 2019 (All Versions)
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
5/2/2011 5:40:07 PM
The WScript object represents the Windows Script Host applications (WScript.exe and CScript.exe). You use this object to get and set certain properties of the scripting host, as well as to access two other objects: WshArguments (the WScript object’s Arguments property) and WshScriptEngine (accessed via the WScript object’s GetScriptEngine method). WScript also contains the powerful CreateObject and GetObject methods, which enable you to work with automation-enabled applications.

Displaying Text to the User

The WScript object method that you’ll use most often is the Echo method, which displays text to the user. Here’s the syntax:

WScript.Echo [Argument1, Argument2,...]

Here, Argument1, Argument2, and so on, are any number of text or numeric values that represent the information you want to display to the user. In the Windows-based host (WScript.exe), the information displays in a dialog box. In the command-line host (CScript.exe), the information displays at the Command Prompt (much like the command-line ECHO utility).

Shutting Down a Script

You use the WScript object’s Quit method to shut down the script. You can also use Quit to have your script return an error code by using the following syntax:

WScript.Quit [intErrorCode]

intErrorCodeAn integer value that represents the error code you want to return

You could then call the script from a batch file and use the ERRORLEVEL environment variable to deal with the return code in some way.

Scripting and Automation

Applications such as Internet Explorer and Word come with (or 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 these applications expose? 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 automation servers, and the applications that manipulate the server’s objects are 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. Windows Live Mail and most of the built-in Windows 7 programs—such as WordPad and Paint—do not expose objects.)

Creating an Automation Object with the CreateObject Method

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

WScript.CreateObject(strProgID)

strProgIDA string that specifies the automation server application and the type of object to create. This string is 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 Word:
 
Word.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 Word’s Application object:

Set objWord = CreateObject("Word.Application")

You need to do nothing else 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. Listing 1 shows a VBScript example (you must have Word installed for this to work).

Listing 1. VBScript Example That Creates and Manipulates a Word Application Object
' Create the Word Application object
'
Set objWord = WScript.CreateObject("Word.Application")
'
' Create a new document
'
objWord.Documents.Add
'
' Add some text
'
objWord.ActiveDocument.Paragraphs(1).Range.InsertBefore "automation test."
'
' Save the document
'
objWord.ActiveDocument.Save
'
' We're done, so quit Word
'
objWord.Quit

This script creates and saves a new Word document by working with Word’s Application object via automation. The script begins by using the CreateObject method to create a new Word Application object, and the object is stored in the objWord variable. From there, you can wield the objWord variable just as though it were the Word Application object.

For example, the objWord.Documents.Add statement uses the Documents collection’s Add method to create a new Word document, and the InsertBefore method adds some text to the document. The Save method then displays the Save As dialog box so that you can save the new file. With the Word-related chores complete, the Application object’s Quit method runs to shut down Word. For comparison, Listing 2 shows a JavaScript procedure that performs the same tasks.

Listing 2. JavaScript Example That Creates and Manipulates a Word Application Object
// Create the Word Application object
//
var objWord = WScript.CreateObject("Word.Application");
//
// Create a new document
//
objWord.Documents.Add();
//
// Add some text
//
objWord.ActiveDocument.Paragraphs(1).Range.InsertBefore("automation test.");
//
// Save the document
//
objWord.ActiveDocument.Save();
//
// We're done, so quit Word
//
objWord.Quit();


Making the Automation Server Visible

The CreateObject method loads the object, but doesn’t display the automation server unless user interaction is required. For example, you see Word’s Save As dialog box when you run the Save method on a new document (as in Listings 30.1 and 30.2). Not seeing the automation server is the desired behavior in most automation situations. However, if you do want to see what the automation server is up to, set the Application object’s Visible property to True, as in this example:

objWord.Visible = True


Working with an Existing Object Using the GetObject Method

If you know that the object you want to work with already exists or is already open, the CreateObject method isn’t the best choice. In the example in the previous section, if Word is already running, the code will start a second copy of Word, which is a waste of resources. For these situations, it’s better to work directly with the existing object. To do that, use the GetObject method:

WScript.GetObject(strPathname [, strProgID])

strPathnameThe pathname (drive, folder, and filename) of the file you want to work with (or the file that contains the object you want to work with). If you omit this argument, you have to specify the strProgID argument.
strProgIDThe programmatic identifier that specifies the automation server application and the type of object to work with (that is, the AppName.ObjectType class syntax).

Listing 3 shows a VBScript procedure that puts the GetObject method to work.

Listing 3. VBScript Example That Uses the GetObject Method to Work with an Existing Instance of a Word Document Object
' Get the Word Document object
'
Set objDoc = WScript.GetObject("C:\GetObject.doc", "Word.Document")
'
' Get the word count
'
WScript.Echo objDoc.Name & " has " & objDoc.Words.Count & " words."
'
' We're done, so quit Word
'
objDoc.Application.Quit

The GetObject method assigns the Word Document object named GetObject.doc to the objDoc variable. After you’ve set up this reference, you can use the object’s properties and methods directly. For example, the Echo method uses objDoc.Name to return the filename and objDoc.Words.Count to determine the number of words in the document.

Note that although you’re working with a Document object, you still have access to Word’s Application object. That’s because most objects have an Application property that refers to the Application object. In the script in Listing 3, for example, the following statement uses the Application property to quit Word:

objDoc.Application.Quit

Exposing VBScript and JavaScript Objects

One of the most powerful uses for scripted automation is accessing the object models exposed by the VBScript and JavaScript engines. These models expose a number of objects, including the local file system. This enables you to create scripts that work with files, folders, and disk drives, read and write text files, and more. You use the following syntax to refer to these objects:

Scripting.ObjectType

Scripting is the automation name of the scripting engine, and ObjectType is the class type of the object.

Note

This section just gives you a brief explanation of the objects associated with the VBScript and JavaScript engines. For the complete list of object properties and methods, refer to http://msdn.microsoft.com/en-us/library/ms950396.aspx.


Programming the FileSystemObject

FileSystemObject is the top-level file system object. For all your file system scripts, you begin by creating a new instance of FileSystemObject:

In VBScript

Set fs = WScript.CreateObject("Scripting.FileSystemObject")

In JavaScript

var fs = WScript.CreateObject("Scripting.FileSystemObject");

Here’s a summary of the file system objects you can access via automation and the top-level FileSystemObject:

  • Drive— This object enables you to access the properties of a specified disk drive or UNC network path. To reference a Drive object, use either the DrivesFileSystemObject object’s GetDrive method. For example, the following VBScript statement references the C: drive: collection (discussed next) or the

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject")
    Set objDrive = objFS.GetDrive("C:")
  • Drives— This object is the collection of all available drives. To reference this collection, use the FileSystemObject object’s Drives property:

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject")
    Set objDrives = objFS.Drives
  • Folder— This object enables you to access the properties of a specified folder. To reference a Folder object, use either the Folders collection (discussed next) or the FileSystemObject object’s GetFolder method:

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFS.GetFolder("C:\My Documents")
  • Folders— This object is the collection of subfolders within a specified folder. To reference this collection, use the Folder object’s Subfolders property:

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFS.GetFolder("C:\Windows")
    Set objSubfolders = objFolder.Subfolders
  • File— This object enables you to access the properties of a specified file. To reference a File object, use either the Files collection (discussed next) or the FileSystemObject object’s GetFile method:

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject")
    Set objFile = objFS.GetFile("c:\autoexec.bat")
  • Files— This object is the collection of files within a specified folder. To reference this collection, use the Folder object’s Files property:

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFS.GetFolder("C:\Windows")
    Set objFiles = objFolder.Files
  • TextStream— This object enables you to use sequential access to work with a text file. To open a text file, use the FileSystemObject object’s OpenTextFile method:

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject")
    Set objTS= objFS.OpenTextFile("C:\Boot.ini")

    Alternatively, you can create a new text file by using the FileSystemObject object’s CreateTextFile method:

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject")
    Set objTS= objFS.CreateTextFile("C:\Boot.ini")

    Either way, you end up with a TextStream object, which has various methods for reading data from the file and writing data to the file. For example, the following script reads and displays the text from C:\Boot.ini:

    Set objFS = WScript.CreateObject("Scripting.FileSystemObject")
    Set objTS = objFS.OpenTextFile("C:\Boot.ini")
    strContents = objTS.ReadAll
    WScript.Echo strContents
    objTS.Close
Other -----------------
- Scripting Windows 7 with WSH : Programming Objects
- Scripting Windows 7 with WSH : Scripts and Script Execution
- Adding Macs to Your Windows 7 Network : Letting Windows Computers See Your Mac Shares
- Adding Macs to Your Windows 7 Network : Using a Mac to Make a Remote Desktop Connection to Windows 7
- Adding Macs to Your Windows 7 Network : Connecting to a Windows Shared Folder
- Adding Macs to Your Windows 7 Network : Connecting to the Windows Network
- Windows 7 : Controlling and Customizing Your Website (part 5) - Viewing the Server Logs
- Windows 7 : Controlling and Customizing Your Website (part 4) - Disabling Anonymous Access
- Windows 7 : Controlling and Customizing Your Website (part 3) - Working Without a Default Document
- Windows 7 : Controlling and Customizing Your Website (part 2) - Setting the Website’s Default Document
- Windows 7 : Controlling and Customizing Your Website (part 1)
- Windows 7 : Adding Folders and Files to the Default Website (part 3) - Adding a Folder to the Default Website
- Windows 7 : Adding Folders and Files to the Default Website (part 2) - Changing the Default Website Home Page
- Windows 7 : Adding Folders and Files to the Default Website (part 1) - Setting Permissions on the Default Website Folder
- Turning Windows 7 into a Web Server : Understanding the Default Website
- Turning Windows 7 into a Web Server : Accessing Your Website
- Windows 7 : Installing Internet Information Services
- Windows 7 : Using Virtual Private Network Connections
- Windows 7 : Using Dynamic DNS to Access Your Network & Configuring a Network Computer for Remote Administration
- Windows 7 : Connecting to a Remote Desktop via the Internet
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS