Using the Component
Next, I need to register the component, which is easy enough. Right-click it in Explorer and select Register from the Context menu. Now I'm ready to use the component in a script.
Showing a Message Box
Listing 26.1 shows the script I wrote that uses the MSG.wsc component.
Listing 26.1. ShowMessageBox.vbs. Displaying a progress bar.
'show the message box
dim oMsgBox
set oMsgBox = createobject("msg.wsc")
oMsgBox.show
oMsgBox.write "Custom text goes here"
oMsgBox.write "Close this box to continue"
oMsgBox.complete
This script should work fine, provided you've downloaded and registered the component.
Showing a Message Box-Explained
This script starts simply enough, by declaring a variable and creating an object reference to the MSG.wsc component that I downloaded and registered.
'show the message box
dim oMsgBox
set oMsgBox = createobject("msg.wsc")
Next, I simply show the message box and use its WriteLine method to display some text.
oMsgBox.show
oMsgBox.write "Custom text goes here"
oMsgBox.write "Close this box to continue"
I then call the component's Complete method. Looking at the component's source code, this appears to actually write all the text I've told it to.
oMsgBox.complete
As you can see, the script component is utilized just like any other object. The difference with the script component is that I can review its source code to see what it does and discover its methods and properties. This is a great way to obtain functionality-like a graphical progress display-that I ordinarily wouldn't want to take the time to write myself.
I don't want to delve into how this particular component functions; the point is that it's very easy to download components like this from the Web and put them into use in your own environment. This particular component is well documented and comes with several examples of how to use it, which is an added bonus. The Web site at http://cwashington.netreach.net/main/default.asp includes several other components that you can download, such as components that provide the following capabilities:
File and folder access. Binary comparisons of files Send a "magic packet" to remote computers to wake them up (Wake-on-LAN) Generate a progress bar display
As I mentioned earlier, you can also use Google to search for script components from other Web sites. Finally, the companion Web site to this book, www.adminscripting.com, hosts a script library where you may find additional useful components (or add your own).
|