|
||||||
Getting Started with Windows Scripting FilesHow to Include JScript, VBScript and PerlScript in a Single ScriptWith 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:
The problem becomes even worse in a multi-developer environment:
Fortunately the solution is quite simple, and that's the use a Windows Scripting file. That's because a Windows Scripting file can:
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 FileThe 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 FileInstead 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 JobNot only can a Windows Scripting file include code from other files in can also contain more than one script job. For that to happen:
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.
|
||||||
|
|
||||||
|
|
||||||