Getting Started with Windows Scripting Files

How to Include JScript, VBScript and PerlScript in a Single Script

© Mark Alexander Bain

Jul 26, 2009
Getting Started with Windows Scripting Files, Mark Alexander Bain
With a Windows Scripting file a programmer no longer has to worry about learning new languages. For example, a VBScript developer can use PerlScript or JScript with ease

When it comes to creating scripts for a Windows computer, there's one question that the application developer must ask, and that's which scripting language they should use. For example, they could possibly use:

  • VBScript
  • JScript
  • PerlScript (if they have installed ActivePerl)

The problem becomes even worse in a multi-developer environment:

  • Should everyone use the same language?
  • Does that mean that some of the team will have to ignore their existing skills and learn a new language?

Fortunately the solution is quite simple, and that's the use a Windows Scripting file. That's because a Windows Scripting file can:

  • work with functionality written in different scripting languages
  • include functionality from multiple files
  • store the code for many scripts in a single file

With a Windows Scripting file it doesn't matter what language a script is written in, or where it is stored, or even how many scripts are stored in the file.

Using Different Programming Languages in a Windows Scripting File

The real power of a Windows Scripting file is that it can combine a number of scripting languages to form a single application.

The file itself is an XML file stored with a .wsf file extension, and all of the code must be encased in a <job>...</job> set of tags. It's then just a matter of creating any required functionality and stating which scripting language is being used for each block of code:

<job>
<script language="PerlScript">
sub hello_pl {
$ip_text = @_[0];
$WScript->Echo($ip_text);
}
</script>
<script language="VBScript">
Sub hello_vbs (ip_text)
wscript.echo ip_text
End Sub
</script>
<script language="JScript">
function hello_js (ip_text) {
wscript.echo (ip_text);
}
hello_pl ("Hello from PerlScript");
hello_vbs ("Hello from VBScript");
hello_js ("And hello from JScript");
</script>
</job>

In this example subroutines have been created using PerlScript, VBScript and JScript, and then those have been run by some JScript code. If this code is saved into a file (for example "helloworld.wsf") then it can be run from the command line by typing:

cscript /nologo helloworld.wsf

The result can be seen in figure 1 at the bottom of this article.

Including Code Files in a Windows Scripting File

Instead of including all of the code in a single file, the file can include the code from other sources. Therefore, if the code for the functions for each scripting language is placed in a separate file then each of those can be included in the final script file, for example:

<job>
<script language="PerlScript" src="functions.pl" />
<script language="VBScript" src="functions.vbs" />
<script language="JScript" src="functions.js" />
<script language="JScript">
hello_pl ("Hello from PerlScript");
hello_vbs ("Hello from VBScript");
hello_js ("And hello from JScript");
</script>
</job>

The end result is the same (as shown in figure2), but the code elements can now be reused in more than one application.

Working with More than One Job

Not only can a Windows Scripting file include code from other files in can also contain more than one script job. For that to happen:

  • the jobs must be contained within a package
  • each job must have a unique id

for example:

<package>
<job id="hello">
<script language="PerlScript" src="functions.pl" />
<script language="VBScript" src="functions.vbs" />
<script language="JScript" src="functions.js" />
<script language="JScript">
hello_pl ("Hello from PerlScript");
hello_vbs ("Hello from VBScript");
hello_js ("And hello from JScript");
</script>
</job>
<job id="hello_vb_only">
<script language="VBScript" src="functions.vbs" />
<script language="VBScript">
hello_vbs "And finally - Hello from VBScript only"
</script>
</job>
</package>

Each job can then be called from the command line:

cscript /nologo //job:hello helloworld.wsf
cscript /nologo //job:hello_vb_only helloworld.wsf

The results of doing this can be seen in figure 3. The output is simple and belies the fact of just how powerful and useful this technique. By using a Windows Scripting files a programmer (or team of programmers) is released from the limitations of working alone and with only one programming language.

Now a programmer can easily make use of any code, regardless of the scripting language being used. And suddenly the diversity of programming languages stops being a barrier and becomes a definite advantage.


The copyright of the article Getting Started with Windows Scripting Files in Windows Programming is owned by Mark Alexander Bain. Permission to republish Getting Started with Windows Scripting Files in print or online must be granted by the author in writing.


Getting Started with Windows Scripting Files, Mark Alexander Bain
Figure 1: A Simple Windows Scripting File, Mark Alexander Bain
Figure 2: Including Windows Scripting Files, Mark Alexander Bain
Figure 3: Windows Scripting with Multiple Jobs, Mark Alexander Bain
 


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo