Scripting Basics

In this lecture we discuss some basic methods and properties of two WSH objects WScript and WScript.WshShell.

WScript object

WScript properies
Properties Description
Application Returns the name of the application (usually "Windows Script Host")
Arguments Object that contains all arguments passed to the script from the command line or by drag-and-drop.
FullName Returns the full name (including path) of the current script host.
Name Returns the name of the current script host.
Path Returns the path to the script.
ScriptFullName Returns the full path to the current script.
ScriptName Returns the name of the current script.
Version Returns the script host version.
This example demonstrates the values of these properties for a simple JScript script.

JScript also provides several built-in functions that allow to get more information about script engine:

Script arguments

Special property Arguments is an object that contains all script arguments. This object has property length that indicates the number of arguments passed to the script. Special method Item() takes one integer argument and returns the argument with the given number. The following script illustrates how to use this property.

WSH provides drag-and-drop mechanism to pass arguments to a script. We can select one or more files with a mouse, drag them to a script you want to execute, and release the mouse button. That executes the script and passes the names of the selected files as arguments for the script.

Methods Quit(), Sleep() and CreateObject()

We have seen method Echo() of the WScript object. In this section we'll discuss several methods of this object:

WScript.WshShell object

The following table contains properties and methods of the object WScript.WshShell:
Methods Properties
CreateShortCut() Environment
ExpandEnvironmentStrings()   SpecialFolders   
LogEvent()  
Popup()  
RegDelete()  
RegRead()  
RegWrite()  
Run()  

To use methods or properties of the object WScript.WshShell we first need ... to create an instance of WScript.WshShell. Unfortunately, there is no a property of the WScript object that we can use to return the WScript.WshShell object. Because of this, we have to use method CreateObject() of the WScript. As we mentioned above this method takes a string argument containing the name of an object to be created. Name that corresponds to the WScript.WshShell object is "WScript.Shell". Thus, to create object we need to use in this section we use code like this:
var ws = WScript.CreateObject("WScript.Shell");

Environment variables

Now, when we created an instance of the WScript.WshShell object we can use method ExpandEnvironmentStrings() to retrieve values environment variables. This method takes a string argument that contains name of the environment variable we need to read enclosed in percent signs. For example, to get the name of the computer we run this script on and the domain name we can use:
var ws = WScript.CreateObject("WScript.Shell");
var cname = ws.ExpandEnvironmentStrings("%computername%");
var dname = ws.ExpandEnvironmentStrings("%domainname%");
WScript.Echo("\\\\"+dname+"\\"+cname);
This following example also shows how to use property Environment to access environment variables.

Popup()

The Popup() method of the WScript.WshShell provides a tool to organize simple dialogs with user. The general syntax of the method is:
Popup(message[, wait] [, title] [, type]);
where
  • message is the message to display in the pop-up window
  • wait is the number of seconds to wait before closing the pop-up window
  • title is the title of the window
  • type is the value that represents the buttons and icons to use in the window
    The following constants can be used to define the buttons we want to see in the pop-up window:
    Buttons for Popup/Message Boxes
    Value   Description
    0 Displays the OK button
    1 Displays OK and Cancel buttons
    2 Displays Abort, Retry, and Ignore buttons
    3 Displays Yes, No, and Cancel buttons
    4 Displays Yes and No buttons
    5 Displays Retry and Cancel buttons
    6 Displays Cancel, Try Again, and Continue buttons

    We can also specify the following icons in the window:
    Icons for Popup/Message Boxes
    Value   Description
    16 Displays an icon with an X, used for critical errors.
    32 Displays an icon with an question mark, used for questions.
    48 Displays an icon with an exclamation point, used for minor errors, caution and warnings.
    64 Displays an icon with an I, used for informational messages (this is the default).
    The following code shows how to give a simple error message with Abort, Retry, Ignore buttons:
    var ws = WScript.CreateObject("WScript.Shell");
    var res = ws.Popup("Drive A: is not ready.", 0, "Error", 2+16);
    
    This method returns the code of the button pressed or -1 if the timeout expired. The table below contains codes of the buttons:
    Button  Return Value      Button  Return Value  
    OK 1 Ignore 5
    Cancel 2 Yes 6
    Abort 3 No 7
    Retry 4 Try again 10
    Continue 11    
    This example allows to play with settings of the Popup() method.

    The method Run()

    The method Run() allows us to execute other applications from Windows scripts. The general syntax of the Run() method is
    Run(command, [win_style], [wait_on_return]);
    
    where
  • command is the name of the program to execute and its arguments
  • win_style is the style of the window of the started program
  • wait_on_return specifies whether the script should wait for the program to end or continue the execution (default).

    The following table contains Windows style options:
    Window Style Options
    Option Description
    0 Runs a program or script in the background.
    1 Runs a program or script normally and displays a window if necessary.
    2 Activates a program and displays it as a minimized window.
    3 Activates a program and displays it as a maximized window.
    4 Activates a program and displays it in its most recent size and position.
    5 Activates a program and displays it in its current size and position.
    6 Minimizes the specified window and activates the next top-level window in the Z order.
    7 Minimizes the program window without activating it.
    8 Displays the program window in its current state but doesn't activate it.
    9 Activates and restores the window. If the window is minimized or maximized, the system restores it to its original size and position.
    10 Sets the display state based on the state of the Window script.

    The following code show how to write a script that loads itself in a Notepad:

    var ws = WScript.CreateObject("WScript.Shell");
    ws.Run("notepad "+WScript.ScriptFullName, 3, true);
    

    This example allows us to play with windows style setting of the Run() method.

    Combining JScript and VBScript

    Format .wsf files

    Here is a short list of some XML tags used in .wsf files: A .wsf file should have at least one job in it. To define a job, we need to use job tag with the body of the job inside. A body of a job is one or more scripts written in either JScript or VVBScript (or other scripting languages). To start a script, we need to use script tag with parameter language to specify the language of the script. Thus, the simplest .wsf file looks like this:
    <job>
    <script language="JScript">
     /* script goes here */
    </script>
    </job>
    

    If we have more that one job in a file we can assign IDs to the jobs. To assign an id, we need to use ID parameter of the tag job. Several different jobs are to be combined in a package; that is, we need to put these jobs inside the package tag. The following file shows two different jobs in a file:

    <package>
    <job id="logon">
     <script language="JScript">
       /* the body of the script should be here */
     </script>
    </job>
    <job id="logoff">
     <script language="VBScript">
      ' the body of the script should be here
     </script>
    </job>
    </package>
    
    Using argument //Job:job_id we can choose which job from a package to execute.

    Tag script can also be used to include script files into a package file. To do that, we need to use parameter src of the script tag:

    <script language="JScaript" src="logon.js" />
    
    Please note the slash before the greater sign. Since we omit the closing tag </script> to satisfy the requirements of the XML format we need to use that slash to show that this tag is both opening and closing at the same time.

    Combining functions of multiple scripting languages

    When programming in different languages, you may often encounter a situation in which you need to use a script implemented in one language inside a code implemented in another. When we use wsf files we can do that. In VBScript we can call JScript functions and in JScript we can execute VBScript subroutines and call VBScript functions. In the example below we are using VB function InputBox, which does not have an analog in JScript to implement a similar function in JScript. The syntax of VBScript function InputBox is
    InputBox(prompt, title, default_value, X, Y)
    
    where prompt is the text to display in the input box, title is the title for the input window, default_value is the value to use when no input is entered, and X and Y are coordinates for the upper-left corner of the input window. This function returns a string entered by the user. Here we have s detailed example that shows how to use VB functions from JScript code.

    In-class assignment

    Develop a script that randomly picks a number between 0 and 100 (included) and then let's the user guess it. The game is going until the user gets the number right. Every time user makes a guess, the script should tell is the number entered is less or greater than the number picked.
    Note: use method random() of the object Math. This method takes no arguments and returns a random number between 0 and 1. Thus, you need to convert it to an integer number between 0 and 100. To get rid of digits after the decimal point you can use method floor() of the same object Math.