Web Development

Examples of uniting VBScript and JScript scripting languages with Windows Script Files

If you need further proof that Windows Script File is a powerful tool in building scripts, then look no further. In part two of a Daily Drill Down series from Greg Shultz, he provides an in-depth look at how to combine two disparate scripting languages.

In part one of my Daily Drill Down series on uniting the VBScript and JScript languages with Windows Script Files, I showed you how to combine code from both languages and explained how to take advantage of this feature when you’re creating scripts. In part two, I’ll provide an example of how to use Windows Script Files to combine sample code from VBScript and JScript languages.

As you look at these examples, keep in mind that you should be focusing on the techniques I’m showing you more than the example scripts. These scripts are not really that practical, but they serve to illustrate the possibilities of combining scripting languages in one Windows Script File quite nicely. Furthermore, if you want to experiment with these techniques, you can download all the example files by clicking here.

When two become one: Combining scripts
To get an idea of how you can combine scripts from the VBScript and JScript scripting languages, let’s suppose that you have two script files for obtaining the version numbers of each of the default scripting language engines. Since each of the scripting engines must be loaded when you run the script, you have one script written in VBScript and another written in JScript. Of course, this means that you have to run each of the files separately.

However, since the new Windows Script File format will allow you to combine scripting languages in one file, you can combine these two separate scripts in one file. Now, since the new Windows Script File format is very flexible, there are a number of ways that we can go about doing so.

The individual scripts
Before I show you how to combine the two script files into one Windows Script File, let’s take a closer look at each of these files individually. Doing so will give you a good idea of how they work and allow you to focus on the task of combining the files when we get to that point.

The Vbv.vbs script shown in Listing A contains the script for obtaining the version number for the VBScript scripting language engine.

Listing A: Vbv.vbs
  1. Dim Vbver, Dummy, Ver
  2. Vbver = GetVBScriptEngineInfo
  3. Dummy = MsgBox(Vbver, 64,"Scripting Engine")
  4. Function GetVBScriptEngineInfo
  5. Ver = "You are using " & ScriptEngine & " Version "
  6. Ver = Ver & ScriptEngineMajorVersion & "."
  7. Ver = Ver & ScriptEngineMinorVersion
  8. GetVBScriptEngineInfo = Ver
  9. End Function

This simple code block begins by declaring three variables with the Dim statement in line 1. Then, in line 2, it calls the GetVBScriptEngineInfo function. This function runs the ScriptEngine, ScriptEngineMajor, and ScriptEngineMinor functions and progressively assigns the values to the Ver variable using the ampersand (&) concatenation operator. When the GetVBScriptEngineInfo function finishes its task, it returns the result to line 2, where it is assigned to the Vbver variable. To complete the operation, line 3 uses the MsgBox function to display the results, as shown in Figure A.

Figure A
The Vbv.vbs script uses the MsgBox function to neatly display the version number of the VBScript scripting language engine.


The Jsv.js script shown in Listing B contains the script for obtaining the version number for the JScript scripting language engine. This script works exactly the same way as the other script, except that it uses the Windows Script Host’s Echo method to display the version number for the JScript scripting language engine, which is shown in Figure B.

Listing B: Jsv.js
  1. var Jsver = GetJScriptEngineInfo();
  2. WScript.Echo(Jsver);
  3. function GetJScriptEngineInfo(){
  4. var Ver = "You are using " + ScriptEngine() + " Version ";
  5. Ver += ScriptEngineMajorVersion() + ".";
  6. Ver += ScriptEngineMinorVersion();
  7. return Ver;
  8. }

Figure B
The Jsv.js script uses the Windows Script Host’s Echo method to display the version number of the JScript scripting language engine.


As you can see, while the Echo method does provide you with a way to display results in a dialog box from JScript, it’s pretty plain when compared to the dialog box created by the VBScript MsgBox function. In one of the upcoming examples, I’ll show you how you can remedy this by calling a VBScript function from within JScript.

Using the two-job package
The first method I’ll show you for combining these two scripts into one Windows Script File is by making each of them a separate job. As you’ll soon see, this isn’t the most efficient way to combine these two particular scripts, but it will serve to illustrate how you go about creating a Windows Script File with two jobs.

The TwoJobPackage.wsf script is shown in Listing C.

Listing C: TwoJobPackage.wsf
  1. <?XML Version="1.0" ?>
  2. <?job error="true" debug="true"?>
  3. <package> 
  4. <job id="VB Code"> 
  5. <script language="VBScript"> 
  6. <![CDATA[ 
  7. Dim Ver, Dummy
  8. Ver = "You are using " & ScriptEngine & " Version "
  9. Ver = Ver & ScriptEngineMajorVersion & "."
  10. Ver = Ver & ScriptEngineMinorVersion
  11. Dummy = MsgBox(Ver, 64,"Scripting Engine")
  12. ]]>
  13. </script>
  14. </job>
  15. <job id="JS Code">
  16. <script language="JScript"> 
  17. <![CDATA[ 
  18. var Ver = "You are using " + ScriptEngine() + " Version ";
  19. Ver += ScriptEngineMajorVersion() + ".";
  20. Ver += ScriptEngineMinorVersion();
  21. WScript.Echo(Ver);
  22. ]]>
  23. </script>
  24. </job>
  25. </package>

As you can see, line 1 simply contains the required XML declaration, which as you’ll remember from part one, specifies that this file will adhere to the rules set forth in version 1.0 of the XML language. Then, line 2 specifies the attributes for error handling which make it possible for the Windows Script Host’s script component compiler to notify you of errors that occur in the XML code at compilation and run time. Keep in mind that these two lines must appear in every Windows Script File.

Since this Windows Script File contains two specific jobs, I must use the <package> tag. As you can see, the package for this specific file begins on line 3 and ends on line 25.

On line 4, I use the <job id> tag to specify that the section of code enclosed between this tag and the closing </job> tag on line 14 is going to be referred to as VB Code. In line 5, I specify that the scripting language for the section of code starting here and ending on line 13 is going to be VBScript. Then, in line 6, I use the opening character data delimiter to isolate the VBScript code from the XML compiler. Line 12 contains the closing delimiter.

Now, the real work begins on line 8. You should recognize the code in lines 8 through 11, as they are the key pieces of code from the Vbv.vbs file. This section of code will create and display the same dialog box shown in Figure A.

The JScript section of code begins with the <job id> tag on line 15, which specifies that the section of code from here to line 24 will be called JS Code. As in the previous job, the <script> tag identifies the scripting language and the character data delimiter isolates the JScript code from the XML compiler. Lines 18 through 21 are the key pieces of code from the Jsv.js file.

Now when you run the TwoJobPackage.wsf script either by double-clicking the file in Windows Explorer or by typing the commands on the DOS command line, you’ll only see the VBScript dialog box. This is because the default execution scheme for a Windows Script File is to only run the first job that it contains. If you want to run the second job in a Windows Script File, you need to specify that job’s ID on the command line.

For example, to run the JScript section of code in the TwoJobPackage.wsf script file, you’ll need to type the command
TwoJobPackage.wsf //job:JS Code

in the Run dialog box, or the command
Cscript TwoJobPackage.wsf //job:JS Code

on the DOS-based command line.

As you can see, in the case of these scripts, using two different jobs in a single Windows Script File is more work than having two separate scripts. However, there may be occasions where having two or more jobs in one Windows Script File may be the best way to use your code. So keep this technique in mind.

Putting everything in one job
The next technique for combining these two scripts into one Windows Script File would be to simply remove the <package> and <job id> tags and put both scripts in one job. The result of doing so is shown in Listing D.

Listing D: OneJob1.wsf
  1. <?XML Version="1.0" ?>
  2. <?job error="true" debug="true"?>
  3. <job> 
  4. <script language="VBScript"> 
  5. <![CDATA[ 
  6. Dim Ver, Dummy
  7. Ver = "You are using " & ScriptEngine & " Version "
  8. Ver = Ver & ScriptEngineMajorVersion & "."
  9. Ver = Ver & ScriptEngineMinorVersion
  10. Dummy = MsgBox(Ver, 64,"Scripting Engine")
  11. ]]>
  12. </script>
  13. <script language="JScript"> 
  14. <![CDATA[ 
  15. var Ver = "You are using " + ScriptEngine() + " Version ";
  16. Ver += ScriptEngineMajorVersion() + ".";
  17. Ver += ScriptEngineMinorVersion();
  18. WScript.Echo(Ver);
  19. ]]>
  20. </script>
  21. </job>

As you can see, this script contains one job that begins on line 3 and ends on line 21. Each individual section of script code is then enclosed in the appropriate script tags and character data delimiters in the same way as in the previous script. However, when you run this script file, you'll first see the VBScript dialog box shown in Figure A. When you click OK to close it, you'll see the JScript dialog box shown in Figure B.

Including the files in a Windows Script File
While you can put the entire contents of each of the two script files in a Windows Script File, as I did previously, it might be more efficient to simply link to and run the original files from within the Windows Script File. When you do, you’re simply using the Windows Script File as a driver to run the individual script files.

The script that performs this operation, OneJob2.wsf, is shown in Listing E.

Listing E: OneJob2.wsf
  1. <?xml Version="1.0" ?>
  2. <?job error="true" debug="true"?>
  3. <job> 
  4. <script language="VBScript" src="Vbv.vbs"> 
  5. </script>
  6. <script language="JScript" src="Jsv.js"> 
  7. </script>
  8. </job>

As you can see, the only lines in the OneJob2.wsf related to the individual script files are the <script> tags that identify the language and use the src attribute to import the contents of the individual script files into the Windows Script File. When you run this file, each script will run and you’ll see the same two dialog boxes shown in Figures A and B.

Including just the functions in a Windows Script File
In this technique, the two script files will only contain the functions. This technique will come in handy if you want to build up a library of functions that you can use from within any Windows Script File.

To begin with, I’ll need to edit the original script files so that they only contain functions. If you look back at Listings A and B, you’ll see that the only thing I need to do to these files is remove lines 1, 2, and 3 from Vbv.vbs and lines 1 and 2 from Jsv.js. I then save them under different names in order to keep the originals available. The new names are Vbv-f.vbs and Jsv-f.js, respectively.

The script that calls just the functions, OneJob3.wsf, is shown in Listing F.

Listing F: OneJob3.wsf
  1. <?xml Version="1.0" ?>
  2. <?job error="true" debug="true"?>
  3. <job>
  4. <script language="VBScript" src="Vbv-f.vbs"> 
  5. <![CDATA[ 
  6. Dim Ver, Dummy
  7. Ver = GetVBScriptEngineInfo
  8. Dummy = MsgBox(Ver, 64,"Scripting Engine")
  9. ]]>
  10. </script>
  11. <script language="JScript" src="Jsv-f.js"> 
  12. <![CDATA[ 
  13. var Ver = GetJScriptEngineInfo();
  14. WScript.Echo(Ver);
  15. ]]>
  16. </script>
  17. </job>

As you can see, the script tags in lines 4 and 11 contain links to the new script files. Since the script files only contain functions, including them doesn’t run them as it did in the previous script. However, the files are now open in memory and available for use. As such, I need to specifically call the functions from lines 7 and 13. I then use the commands in lines 8 and 14 to display the results obtained from the external functions.

For example, in line 7, I call the GetVBScriptEngineInfo function and assign the result to the Ver variable. I then use the MsgBox function on line 8 to display the results.

Sharing functions
As I mentioned earlier, while the Echo method does provide you with a way to display results in a dialog box from JScript, it’s pretty plain when compared to the dialog box created by the VBScript MsgBox command. Fortunately, one of the features that the Windows Script File makes possible is the sharing of features from one scripting language to another. As such, you can remedy this problem by creating a function in VBScript that displays output with the MsgBox function. You can then call this VBScript function from within a section of JScript code. The OneJob4.wsf file in Listing G demonstrates this technique.

Listing G: OneJob4.wsf
  1. <?xml Version="1.0" ?>
  2. <?job error="true" debug="true"?>
  3. <job> 
  4. <script language="VBScript" src="Vbv-f.vbs"> 
  5. <![CDATA[
  6. Dim Ver, Dummy 
  7. Ver = GetVBScriptEngineInfo
  8. DisplayIt(Ver)
  9. Function DisplayIt(Ver)
  10. Dummy = MsgBox(Ver, 64,"Scripting Engine")
  11. End Function
  12. ]]>
  13. </script>
  14. <script language="JScript" src="Jsv-f.js"> 
  15. <![CDATA[ 
  16. var Ver = GetJScriptEngineInfo();
  17. DisplayIt(Ver);
  18. ]]>
  19. </script>
  20. </job>

This version of my example Windows Script File uses the same technique of including the script files that just contain the functions. In line 7, I call the GetVBScriptEngineInfo function and assign the result to the Ver variable. Then, in line 8, I call the internal DisplayIt function and send it the results stored in the Ver variable. Lines 9 through 12 make up the DisplayIt function, which simply contains the MsgBox command that displays the results.

The advantage is that once I get the results from the JScript function in line 16, I can then call the VBScript function DisplayIt and send it the results stored in the JScript version of the Ver variable. The results from the JScript function are then displayed in the nicely formatted dialog box made possible by the VBScript MsgBox function, as shown in Figure C.

Figure C
By encapsulating the MsgBox command in a function in the VBScript section of the Windows Script File, I can call that function from the JScript section of the Windows Script File.


Conclusion
The Windows Script File format introduced in Windows Script Host 2.0, which is basically an XML file format, introduces all sorts of new features to the Windows Script Host. In this Daily Drill Down series, I’ve introduced you to the XML features in the Windows Script File format and shown you how to take advantage of the fact that you can now combine both VBScript and JScript code into one file.

About

Greg Shultz is a freelance Technical Writer. Previously, he has worked as Documentation Specialist in the software industry, a Technical Support Specialist in educational industry, and a Technical Journalist in the computer publishing industry.

0 comments

Editor's Picks