Newsletters #1

Here is my first newsletter dealing with the WSH Bazaar and WSH topic.

How to retrieve an Input in JScript

Many newbies tries (like the author a year ago) to find an Input method for JScript. Whilst VBScript supports the InputBox function within the language, JScript doesn't comes with any feature allowing a user input. I could not believe it, but the prompt method used within the Internet Explorer is a method provided from the Internet Explorer's document object.

Therefore one of my first tasks was to implement my own method to retrieve an input either in JScript or in VBScript. The method is supported in the WSHExtend ActiveX-Control and may be used with the following statements:

var objAdr = WScript.CreateObject("WSHExtend.WinExt");
var name = objAdr.WSHInputBox ("Please enter your name", 
           "JScript user input", "Brown");
WScript.Echo ("You entered: ", name);

These 3 statements retrieve a reference to the WSHExtend WinExt object. Then the WSHInputBox method is invoked to retrieve a user input. The result is shown using the Echo method. Further information may be found in the WSHExtend Programmer's Reference.

How to control Windows application which doesn't support objects?

Many Windows applications doesn't support objects, methods and properties. Let's take the Windows editor for example. This tool doesn't provide an application object like Microsoft Word or other applications. In some cases it comes handy to invoke the Windows editor using the Run method of the Shell object. After you have the editor's window in foreground, you need something like a SendKeys function. Whilst Microsoft Office VBA programmers knows such a function, WSH provides nothing for that. Therefore I have implemented a WSHSendKeys method within the WSHExtend control. The following JScript code sequence demonstrates how to use the new feature:

// first retrieve the Shell object
var WSHShell = WScript.CreateObject("WScript.Shell");
// launch Windows editor
WSHShell.Run ("Notepad.exe");
// now retrieve the WinExt object
var Wsh = WScript.CreateObject("WSHExtend.WinExt");

for (var i = 1; i < 1000, i ++)
 { var j = i * i }                 // just wait a little bit
// now send some text to the editor window
Wsh.WSHSendKeys ("Hello World \n", true);
Wsh.WSHSendKeys ("10", true);
Wsh.WSHSendKeys ("{+}", true);
Wsh.WSHSendKeys ("2", true);
Wsh.WSHSendKeys ("=", true);
Wsh.WSHSendKeys ("12", true);

This sequence causes the editor to display some text within the window. Using WSHSendKeys allows you also to control a whole application (sending shortcuts, tampering with the clipboard and more). My WSH book discusses the details of this method and some samples. Further samples and details may be found also in my WSH Bazaar. Have a look at the sample page and at the WSHExtend Programmers Reference.

How to wait within a script?

One problem during script programming deals with a script delay. Also the code sequence discussed above contains a delay loop to wait untill the Windows editor window becomes the focus. Otherwise the WSHSendKeys keystrokes are send to the "wrong" window. The penalty we have to pay using the for loop given above are: the delay depends on your machine, and this loop spikes the CPU load to 100 %.

To avoid all this trouble, I have implemented a WSHWait method within the WSHExtend control. This method may be called using the following code sequence:

var Wsh = WScript.CreateObject("WSHExtend.WinExt");
Wsh.WSHWait(1000);     // Wait one second

The WSHWait method requires one parameter containing the delay time in Milliseconds. This method suspends the script's execution for the give time. After the delay elapses, the script execution continues. Further samples and details may be found also in my WSH Bazaar. Have a look at the sample page and at the WSHExtend Programmers Reference.


(c) G. Born