Tải bản đầy đủ (.pdf) (10 trang)

Microsoft WSH and VBScript Programming for the Absolute Beginner Part 45 pot

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (84.69 KB, 10 trang )

420
Writing the Second JScript
The final script defined in this Windows Script File, shown next, is JScript. It displays the
game’s closing splash screen and is designed in the same basic manner as the first JScript,
using the
WshShell object’s Popup() method to display its graphical pop-up dialog.
//******************************************************************
//Script Name: N/A
//Author: Jerry Ford
//Created: 12/20/02
//Description: This JScript displays the .WSF file’s closing splash
// screen
//******************************************************************
//Initialization Section
var objWshShl = WScript.CreateObject(“WScript.Shell”);
var strMessage;
var strAuthor;
var dtmDate;
var strTitleBarMsg;
strMessage = “Thank you for using the VBScript Game Console © “;
strAuthor = “Jerry Ford “;
dtmDate = “2002”;
//Main Processing Section
strTitleBarMsg = getResource(“cTitlebarMsg”);
//Display a popup dialog using the WshShell object’s Popup() method
objWshShl.Popup(strMessage + strAuthor + dtmDate, 0, strTitleBarMsg);
The Final Result
That’s it. Let’s assemble the entire Windows Script File as shown next. Before you run it for
the first time, be sure that you’ve first created a folder called
C:\VBScriptGames and that
you’ve copied both the VBScript game console script as well as a few other of your VBScript


games into the folder.
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
<package>
<comment>This .WSF file builds a VBScript Game Console</comment>
<job>
<resource id=”cTitlebarMsg”>VBScript Game Console</resource>
<script language=”JScript”>
//*******************************************************************
//Script Name: N/A
//Author: Jerry Ford
//Created: 12/20/02
//Description: This JScript displays the .WSF file’s initial splash
// screen
//*******************************************************************
//Initialization Section
var objWshShl = WScript.CreateObject(“WScript.Shell”);
var strWelcome;
var strInstructions;
var intResults;
var intReply;
var strTitleBarMsg;
strWelcome = “Welcome to the VBScript Game Console. “;
strInstructions = “Click on OK to play a VBScript game!”;
//Main Processing Section
//Verify that the user wants to open the VBScript Game Console
intReply = DisplayInitialSplashScreen();
//intReply will be set equal to 2 if the user clicks on the Cancel
if (intReply == 2) {
421
Chapter 12 • Combining Different Scripting Languages

422
//Close the VBScript Game Console
WScript.Quit();
}
//Procedure Section
//This procedure prompts the user for confirmation
function DisplayInitialSplashScreen() {
strTitleBarMsg = getResource(“cTitlebarMsg”);
//Display popup dialog using the WshShell object’s Popup() method
intResults = +
objWshShl.Popup(strWelcome +
strInstructions, 0, strTitleBarMsg, 1);
//Return the result to the calling statement
return intResults
}
</script>
<script language=”VBScript”>
‘*******************************************************************
‘Script Name: N/A
‘Author: Jerry Ford
‘Created: 12/20/02
‘Description: This VBScript displays the actual VBScript
‘ Game Console interface and interacts with the user
‘*******************************************************************
‘Initialization Section
Option Explicit
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
Dim objFsoObject, objWshShl, strPlayOrNot, strConsoleStatus
Dim objGameFolder, objGames, strSelection, objWordList
Dim strFileString, intCount, strDisplayString, intNoFilesFound

Dim strTitleBarMsg, intResults
Dim ConsoleArray()
‘Set up an instance of the FileSystemObject
Set objFsoObject = CreateObject(“Scripting.FileSystemObject”)
‘Set up an instance of the WshShell
Set objWshShl = WScript.CreateObject(“WScript.Shell”)
‘Retrieve the titlebar message to the display in popup dialogs
strTitleBarMsg = getResource(“cTitlebarMsg”)
‘Main Processing Section
If intResults = 2 Then
WScript.Quit()
End If
‘Specify the location of the folder where word files are stored
Set objGameFolder = objFsoObject.GetFolder(“C:\VBScriptGames”)
‘Get a list of files stored in the folder
Set objGames = objGameFolder.Files
‘Look and count the number of word files
For Each objWordList In objGames
intNoFilesFound = intNoFilesFound + 1
Next
‘Redefine the script’s array based on number of word files found
ReDim ConsoleArray(intNoFilesFound)
‘Call the function that displays the VBScript Game Console
ConsoleLoop()
423
Chapter 12 • Combining Different Scripting Languages
424
‘Procedure Section
‘This function displays the VBScript Game Console, accepts user
‘input, validates the input and starts other VBScript games

Function ConsoleLoop()
‘This string contains a list of all the word files discovered
‘in the target folder
strSelection = “”
‘This counter will be used to track individual word files and
‘will be kept in sync with array entries
intCount = 0
‘Loop through the list of word files
For Each objWordList In objGames
‘Build a master string containing a list of all the word files
‘But exclude the VBScriptGameConsole.wsf file from this list
If objWordList.Name <> “VBScriptGameConsole.wsf” Then
‘Increment count each time through the loop
intCount = intCount + 1
strFileString = strFileString & “ “ & objWordList.Name
‘Build another list, adding number for later display
strSelection = strSelection & intCount & “. “ & _
objWordList.Name & vbCrLf
‘Load the name of each script into the array
ConsoleArray(intCount) = objWordList.Name
End If
Next
‘This variable is used to determine when to close the console
strConsoleStatus = “Active”
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
‘Create loop & keep it running until the user decides to close it
Do Until strConsoleStatus = “Terminate”
‘Interrogate the user’s input
strPlayOrNot = UCase(PickAGame())
‘If the user did not type anything or if he or she clicked on

‘Cancel then exit the function let things come to an end
If strPlayOrNot = “” Then
Exit Function
End If
‘Define a Select Case statement and use it to test the various
‘possible types of user input
Select Case UCase(strPlayOrNot)
‘If the user typed QUIT then exit the function let things
‘come to an end
Case “QUIT”
Exit Function
‘If the user typed ABOUT call the function that displays
‘additional information about the VBScript Game Console
Case “ABOUT”
AboutFunction()
‘If the user typed HELP call the function that provides
‘additional help information
Case “HELP”
HelpFunction()
‘Otherwise call the function that runs the selected VBScript
Case Else
ValidateAndRun()’
End Select
Loop
End Function
425
Chapter 12 • Combining Different Scripting Languages
426
‘This function validates user input and if appropriate calls
‘functions that display further instructions or runs the selected

‘VBScript
Function ValidateAndRun()
‘Check to see if the user provided a valid game number
If IsNumeric(strPlayOrNot) <> 0 Then
‘Make sure that the user did not type a negative number
If strPlayOrNot > 0 Then
‘Make sure that the use did not type a invalid number
If CInt(strPlayOrNot) < CInt(intCount) Then
‘If the number is valid then find the associated script
strPlayOrNot = ConsoleArray(strPlayOrNot)
‘Call the procedure that will then run the selected script
RunScript()
Else
‘Call this procedure if the user has not typed a valid
‘script number
InvalidChoice()
End If
Else
InvalidChoice()
End If
‘Check to see instead if the user provided a valid game name
Else
‘Proceed only if the input typed by the user a valid VBScript
‘game (e.g. its name appears in the previously built list of
‘VBScript game names
If InStr(1, strSelection, strPlayOrNot, 1) > 1 Then
‘If the user didn’t type the .vbs file extension add it
If InStr(1, strPlayOrNot, “.VBS”, 1) = 0 Then
strPlayOrNot = strPlayOrNot & “.vbs”
‘Recheck to make sure that the script name is still valid

If InStr(1, strSelection, strPlayOrNot, 1) > 1 Then
‘Call the procedure that runs the selected script
RunScript()
Else
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
‘Call this procedure if the user has not typed a valid
‘script name
InvalidChoice()
End If
Else
‘If the user specified the script’s .vbs file extension and
‘it is found in the previously built list of VBScript game
‘names then go ahead and call the procedure that will run
‘the script
If InStr(1, strSelection, strPlayOrNot, 1) > 1 Then
RunScript()
Else
‘Run this procedure if user fails to supply valid input
InvalidChoice()
End If
End If
Else
‘If user supplied input is not found in the previously
‘built list of VBScript game names call this procedure
InvalidChoice()
End If
End If
End Function
‘This function displays the main VBScript game Console and collects
‘user input

Function PickAGame()
strDisplayString = strSelection & vbCrLf & _
“Or Type: [Help] [About] [Quit]” & vbCrLf
PickAGame = InputBox(“W e l c o m e t o t h e” & vbCrLf & _
vbCrLf & “V B S c r i p t G a m e C o n s o l e !” & _
vbCrLf & vbCrLf & “Pick a Game:” & vbCrLf & vbCrLf & _
strDisplayString, strTitleBarMsg, “”, 50, 50)
End Function
427
Chapter 12 • Combining Different Scripting Languages
428
‘This function starts the VBScript selected by the user
Function RunScript()
objWshShl.Run “WScript “ & strPlayOrNot
End Function
‘This function is called whenever the user provides invalid input
Function InvalidChoice()
MsgBox “Sorry. Your selection was not valid. A valid “ & _
“selection consists of one of the following:” & vbCrLf & _
vbCrLf & “* The number associated with one of the listed “ & _
“VBScript games” & vbCrLf & “* The name of a listed “ & _
“VBScript game” & vbCrLf & “* The name of a listed “ & _
“VBScript game plus its file extension” & vbCrLf & _
“* Help - To view help information.” & vbCrLf & _
“* About - To view additional information about this game “ & _
“and its Author” & vbCrLf & “* Quit - To close the “ & _
“VBScript Game Console” & vbCrLf & vbCrLf & _
“Please try again.”, , strTitleBarMsg
End Function
‘This function displays help information in a popup dialog

Function HelpFunction()
MsgBox “Additional help information for the VBScript Game “ & _
“Console can be found at:” & vbCrLf & vbCrLf & _
“www.xxxxxxxx.com.”, , strTitleBarMsg
End Function
‘This function displays information about the VBScript Game Console
‘and its author
Function AboutFunction()
MsgBox “VBScript Game Console © Jerry Ford 2002” & vbCrLf & _
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
vbCrLf & “Email the author at: ”, , strTitleBarMsg
End Function
</script>
<script language=”JScript”>
//******************************************************************
//Script Name: N/A
//Author: Jerry Ford
//Created: 12/20/02
//Description: This JScript displays the .WSF file’s closing splash
// screen
//******************************************************************
//Initialization Section
var objWshShl = WScript.CreateObject(“WScript.Shell”);
var strMessage;
var strAuthor;
var dtmDate;
var strTitleBarMsg;
strMessage = “Thank you for using the VBScript Game Console © “;
strAuthor = “Jerry Ford “;
dtmDate = “2002”;

//Main Processing Section
strTitleBarMsg = getResource(“cTitlebarMsg”);
//Display a popup dialog using the WshShell object’s Popup() method
objWshShl.Popup(strMessage + strAuthor + dtmDate, 0, strTitleBarMsg);
</script>
</job>
</package>
429
Chapter 12 • Combining Different Scripting Languages

×