1. Trang chủ
  2. » Công Nghệ Thông Tin

Microsoft WSH and VBScript Programming for the Absolute Beginner Part 12 ppt

10 335 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 188,91 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Present the player with the equation and then test the player’s response to determine whether he or she provided an answer and whether that answer was numeric.. ‘Script Name: Mathgame.vb

Trang 1

In the Real World Because opening another window while the SendKeys() method is executing will divert the keystrokes to the new window, you will want to find another way of integrating your scripts with other applications whenever possible Many applications, such as Excel and Word, provide their own built-in core object model WSH scripts can interact directly with these applications

by first instantiating references to the application’s objects and then accessing their methods and properties The only trick here is that you need to know the objects that make up the appli-cation’s object model, as well as their associated methods and properties You can often get this information from the application vendor’s Web site or from searching the Internet Of course, if the application that you want to work with does not expose an object model for your scripts

to work with, you can always try using the SendKeys() method

The syntax of the SendKeys()method is as follows:

SendKeys(string)

Stringis a value representing the keystrokes that are to be sent to the target application You can send more keystrokes by simply typing them out, like this:

SendKeys “I am “

SendKeys 38

SendKeys “ years old.”

However, in many cases, you’ll want to send other types of keystrokes For example, to send

an Enter key keystroke, you’ll need to send the following:

SendKeys “~”

Table 3.14 provides a list of SendKeys()keystrokes that you’re likely to want to use

TA B L E 3 14 SE N DKE Y S( ) KE Y S T R O K E S

Trang 2

Key Corresponding SendKeys() Codes

TA B L E 3 14 SE N DKE Y S( ) KE Y S T R O K E S (C O N T I N U E D)

Trang 3

Besides the keystrokes outlined in Table 3.14, Table 3.15 lists three additional keystroke com-binations that can be used to send keystrokes that require a special key to be pressed in con-junction with another key For example, if you were working with an application that could

be closed by holding down the Alt key and pressing the F4 key, you could perform this oper-ation as follows:

SendKeys “%{F4}”

Designing the Game

Okay, let’s start building the Math Game This game will be assembled in five steps The first three steps create the logic that interacts with the user and plays the game The last two steps perform the game’s application demonstrations These steps are as follows:

1 Add the standard documentation template and define any variables, constants, or objects used by the script.

2 Present the player with the equation and then test the player’s response to determine whether he or she provided an answer and whether that answer was numeric.

3 Test to see whether the player provided the correct answer If not, then offer to show the user how to arrive at the correct answer.

4 Add the statements required to start and control the WordPad application.

5 Add the statements required to start and control the Calculator application.

Beginning the Math Game

Let’s begin by adding the script template that I introduced earlier in this chapter This includes initializing variables and constants, and setting up object declaration statements

TA B L E 3 15 SP E C I A L SE N DKE Y S( ) KE Y S T R O K E S

Trang 4

‘Script Name: Mathgame.vbs

‘Author: Jerry Ford

‘Created: 02/28/02

‘Description: This script prompts the user to solve a mathematical

‘expression and demonstrates how to solve it in the event that the user

‘cannot

‘*************************************************************************

‘Initialization Section

Option Explicit

Dim WshShl, QuestionOne, ProveIt

‘Define the title bar message to be displayed in the script’s

‘pop-up dialog

Const cTitlebarMsg = “The Math Game”

‘Instantiate an instance of the WshShell object

Set WshShl = WScript.CreateObject(“WScript.Shell”)

Collect the Player’s Answer and Test for Errors

Next, display the equation and store the player’s answer in a variable called QuestionOne,

like this:

‘Present the player with the equation

QuestionOne = InputBox(“What is the sum of 1 + 5 * 9 / 3 ?”, cTitlebarMsg)

Now verify that the player actually typed in an answer instead of just clicking on OK or Cancel;

if the player has not typed in an answer, display an error message and end the game.

‘See if the player provided an answer

If Len(QuestionOne) = 0 Then

MsgBox “Sorry You must enter a number to play this game.”

WScript.Quit

End If

Trang 5

Another good test to perform is to make sure that the player is, in fact, typing in a number

as opposed to a letter or other special character:

‘Make sure that the player typed a number

If IsNumeric(QuestionOne) <> True Then

MsgBox “Sorry You must enter a number to play this game.”

WScript.Quit

End If

Check for the Correct Answer

Okay, now add a test to see if the player provided the correct answer If the answer provided

is correct, then compliment the player’s math skills Otherwise, offer to teach the player how to solve the equation.

‘Check to see if the player provided the correct answer

If QuestionOne = 16 Then

MsgBox “Correct! You obviously know your math!”

Else

ProveIt = MsgBox(“Incorrect Do you want to see me solve the “ & _

“equation?”, 36, cTitlebarMsg)

If ProveIt = 6 Then ‘Player wants to see the solution

End If

End If

As you can see, I left space in the previous statements This space marks the spot where the rest of the script’s statements will be written as you continue to develop the script.

Interacting with WordPad

For the script to work with the WordPad application, WordPad must first be started This can

be done using the WshShellobject’s Run()method.

WshShl.Run “WordPad”

Trang 6

It may take a moment or two for the application to finish starting, so pause the script’s exe-cution for two seconds and wait using the WScriptobject’s Sleep()method, like this:

WScript.Sleep 2000

Next add a series of statements that use the SendKeys()method to write text to WordPad To slow things down a bit and make the process run more like a slide show, add the Sleep()

method after each write operation Finally, pause for a couple seconds and then close WordPad.

WshShl.SendKeys “To answer this question you must follow the “ & _

“correct order of precedence when performing your calculations.”

WScript.Sleep 2000

WshShl.SendKeys “~~”

WshShl.SendKeys “1st, working from left to right multiply 5 * 9.”

WScript.Sleep 2000

WshShl.SendKeys “~~”

WshShl.SendKeys “2nd, divide the result by 3.”

WScript.Sleep 2000

WshShl.SendKeys “~~”

WshShl.SendKeys “3rd, add 1.”

WScript.Sleep 2000

WshShl.SendKeys “~~”

WshShl.SendKeys “The final answer is 16.”

WScript.Sleep 2000

WshShl.SendKeys “~~”

WshShl.SendKeys “~~”

WshShl.SendKeys “In case you question my math watch this!”

WScript.Sleep 2000

WshShl.SendKeys “%{F4}”

WshShl.SendKeys “%{N}”

Take notice of the last two statements The first statement closed WordPad by sending the F4 keystroke As a new document was just opened, WordPad displays a dialog asking if you want to save it The last statement responds by sending the CTRL+N keystrokes indicating a

“no” response.

Trang 7

Interacting with the Calculator

The final piece of the game opens the Windows Calculator application and resolves the equation, just in case the player has any doubts as to the answer you presented using Word-Pad The statements required to write this portion of the script are as follows:

‘Start the Calculator application

WshShl.Run “Calc”

‘Use the Calculator application to solve the equation

WScript.Sleep 2000

WshShl.SendKeys 5 & “{*}”

WScript.Sleep 2000

WshShl.SendKeys 9

WScript.Sleep 2000

WshShl.SendKeys “~”

WScript.Sleep 2000

WshShl.SendKeys “{/}” & 3

WScript.Sleep 2000

WshShl.SendKeys “~”

WScript.Sleep 2000

WshShl.SendKeys “{+}” & 1

WScript.Sleep 2000

WshShl.SendKeys “~”

WScript.Sleep 2000

WshShl.SendKeys “%{F4}”

As you can see, the same techniques have been used here to work with the Windows Calcu-lator as were used to control WordPad.

The Final Result

Well, that’s it Let’s assemble all the pieces of the script and see what it looks like:

‘*************************************************************************

‘Script Name: Mathgame.vbs

‘Author: Jerry Ford

‘Created: 02/28/02

‘Description: This script prompts the user to solve a mathematical

‘expression and demonstrates how to solve it in the event that the user

‘cannot

‘*************************************************************************

Trang 8

‘Initialization Section

Option Explicit

Dim WshShl, QuestionOne, Proveit

‘Define the title bar message to be displayed in the script’s

‘pop-up dialog

Const cTitlebarMsg = “The Math Game”

‘Instantiate an instance of the WshShell object

Set WshShl = WScript.CreateObject(“WScript.Shell”)

‘Present the player with the equation

QuestionOne = InputBox(“What is the sum of 1 + 5 * 9 / 3 ?”, cTitlebarMsg)

‘See if the player provided an answer

If Len(QuestionOne) = 0 Then

MsgBox “Sorry You must enter a number to play this game.”

WScript.Quit

End If

‘Make sure that the player typed a number

If IsNumeric(QuestionOne) <> True Then

MsgBox “Sorry You must enter a number to play this game.”

WScript.Quit

End If

‘Check to see if the player provided the correct answer

If QuestionOne = 16 Then

MsgBox “Correct! You obviously know your math!”

Else

ProveIt = MsgBox(“Incorrect Do you want to see me solve the “ & _

“equation?”, 36, cTitlebarMsg)

If ProveIt = 6 Then ‘Player wants to see the solution

Trang 9

‘Start the WordPad application

WshShl.Run “WordPad”

‘Pause script execution to give Windows enough time to load WordPad

WScript.Sleep 2000

‘Use WordPad to show the player how to solve the equation

WshShl.SendKeys “To answer this question you must follow the “ & _

“correct order of precedence when performing your calculations.”

WScript.Sleep 2000

WshShl.SendKeys “~~”

WshShl.SendKeys “1st, working from left to right multiply 5 * 9.”

WScript.Sleep 2000

WshShl.SendKeys “~~”

WshShl.SendKeys “2nd, divide the result by 3.”

WScript.Sleep 2000

WshShl.SendKeys “~~”

WshShl.SendKeys “3rd, add 1.”

WScript.Sleep 2000

WshShl.SendKeys “~~”

WshShl.SendKeys “The final answer is 16.”

WScript.Sleep 2000

WshShl.SendKeys “~~”

WshShl.SendKeys “~~”

WshShl.SendKeys “In case you question my math watch this!”

WScript.Sleep 2000

WshShl.SendKeys “%{F4}”

WshShl.SendKeys “%{N}”

‘Start the Calculator application

WshShl.Run “Calc”

‘Use the Calculator application to solve the equation

WScript.Sleep 2000

WshShl.SendKeys 5 & “{*}”

WScript.Sleep 2000

WshShl.SendKeys 9

Trang 10

WScript.Sleep 2000

WshShl.SendKeys “~”

WScript.Sleep 2000

WshShl.SendKeys “{/}” & 3

WScript.Sleep 2000

WshShl.SendKeys “~”

WScript.Sleep 2000

WshShl.SendKeys “{+}” & 1

WScript.Sleep 2000

WshShl.SendKeys “~”

WScript.Sleep 2000

WshShl.SendKeys “%{F4}”

End If

End If

I suggest that you run and test this script to make sure that it works as expected For exam-ple, try typing in a letter instead of a number for the answer Then try typing nothing at all and just click on OK or Cancel Finally, try both a correct and then an incorrect answer and see what happens This is a fairly lengthy script, so the odds of typing it in correctly the first time are slim If you get errors when you run the script, read them carefully and see if the error message tells you what’s wrong and then go fix it Otherwise, you may need to double-check your typing again.

As your scripts grow more complex, you’re going to run into more and more errors while developing them I recommend that you learn to develop your scripts in a modular fashion, writing one section at a time and then testing it before moving on to the next section In Chapter 9, “Handling Script Errors,” I’ll demonstrate how to do this.

Summary

In this chapter, you learned about the core and run-time VBScript objects and their associ-ated properties and methods, and were shown how to use them within your VBScripts You also learned about VBScript syntax, reserved words, and special characters In addition, you learned about and saw the power and convenience of VBScript functions Finally, you learned four different ways to display script output

T R I C K

Ngày đăng: 03/07/2014, 18:20

TỪ KHÓA LIÊN QUAN