WSH, COM, and Scripting Languages
There is always a little confusion when first starting out with a new language
or development tool. WSH is no different. Hopefully this article will help
clear up some of the fog.
WSH can be broken into really three parts.
- WSH Engine
- Scripting Language
- COM Objects
WSH Engine
The WSH engine consists of two executables, WScript.exe and CScript.exe. These
executables take your script name as a parameter and basically hands off your
script to the appropriate script engine. Depending on what you are trying
to accomplish will dictate which one to use.
WScript.exe - Is used for Windows based scripts.
CScript.exe - Is used for console based scripts.
Syntax for using either WScript.exe or CScript.exe
WScript.exe yourscript.js
CScript.exe yourscript.js
Installing WSH maps files with the JS or VBS
extension to WScript.exe. This allows you to double click on a script file
and execute it.
WSH provides you with a way to set the script properties before you execute it.
This is handled by a file with a WSH extension which is
similar in function to the old PIF files.
You can easily create a WSH file by right clicking on yourscript.js,
go to properties, select scripts tab. Make your changes and you will notice a file created
with the same name as your original file with a wsh extension. Instead of executing
yourscript.js you would execute yourscript.wsh.
WScript.exe also provides two objects that you will need WScript and WshArguments.
Scripting Languages
After you install WSH on your machine, you will have access to two scripting
languages, JScript and VBScript. If you prefer to use another scripting language
like Perl and Python, these can be registered with WSH.
You have a choice on what language you decide to use. I imagine that the debate
will rage on for years as to what language is better. I decided on JScript because
I can use the same language for web page scripting and well as WSH scripting.
When writing scripts in a particular language, you have access to all the built in objects
of that language. I highly recommend downloading either the JScript or VBScript documentation
from Microsoft. It is contained in a
convenient HTML Help File.
You should also be aware of which version of a scripting language
you are using. For example, Microsoft added try..catch
to JScript V5, allowing you to handle errors. Likewise, they added regular
expressions to VBScript V5.
You can upgrade you current scripting engine to the latest version
by either downloading IE5 or simply download the Script Engines from
Microsoft's Scripting Site.
COM Objects
The true power of WSH lies in its ability to instantiate (create) COM Objects.
This gives WSH access to a large number of existing COM Components and an easy
way to add functionality to your scripts.
When you install WSH on your computer, WSHOM.OCX is installed. This component
gives you access to several objects and their associated methods and properties.
These include
- WshShell
- WshNetwork
- WshCollection
- WshEnvironment
- WshShortcut
- WshSpecialFolders
- WshUrlShortcut
You may want to develop scripts that require access to objects not provided by
WSHOM.OCX. For example, let say you wanted to control Outlook 98 from a
WSH script. First you create a instance of Outlook using the following
syntax.
olObj = new ActiveXObject("Outlook.Application");
Now you can get/set properties, as well as, call exposed methods.
You can extend the capabilities of WSH by writing your own COM components.
COM is language independent, but C++, Java, and VB are common languages to
develop COM. If you are not up to the task of tackling writing COM components
just yet, there are many 3rd party sources.
Believe it or not, you may already have the COM component you are looking for installed
on your computer. Using a utility called OLEViewer, you can browse you computer
to see what you already have. I will cover using this tool and others in a later
article.
Daren