Guy's Scripting Ezine 26 - VBScript SendKeysContents for Guy's Scripting Ezine 26 - Run, SendKeys and Sleep
This Week's Secret - Everything Has a PurposeI will let you into a secret. Readers prefer me to take a view about a topic rather than sit on the fence. In the case of VBScript, I once wrote that WSCript.Sleep was useless - why would you ever want a script to wait? Well, when I 'take a view' someone will invariably take a contrary position, we get a good discussion. In truth, I sometimes change my view, especially when I remember this: everything has a use under the right circumstances. Therefore, in the case of WSCript.Sleep, I have made an about turn, I now realise that on occasions a script can run faster than the program that they are controlling. By employing the command WScript.Sleep you can make the script wait until the program is ready for the next instruction. The benefit is no error message, and the script achieves its purpose. Example 1 - Creating a VBscript to Open Notepad (Or any other program)This week is your chance to have a bit of fun, to learn just by 'messing about' with scripts. My point is that this example of opening Notepad.exe is not a crucial script, but it does give valuable practice with the .run and .SendKeys methods. My script works at two levels. At the practical level, VBScript will open, run, or execute a program. At the theoretical level, you will learn about the WshShell.Run method and also at a practical level, discover when to pause scripts with Wscript.Sleep command. As a bonus, you will have a chance to manipulate text within your program thanks to WshShell.SendKeys. Warning I hope that you have spotted, I am not a great one for long disclaimers, thus all the more reason to take note of my warning about SendKeys. Most of the time SendKeys works as expected. However, if you had the proverbial barrow load of monkeys, all using SendKeys, then one of them would do something drastic like over-write a vital report that just happen to be open. My advice is: take sensible precautions, close all unnecessary programs before experimenting with SendKeys. One other point may strike, you, if SendKeys is a liability in testing, what would it be like in a production script. My answer is that SendKeys is only a last resort, or an intermediate step until you develop a better method. Instructions for opening Notepad
' SendKeys.vbs Learning PointsNote 1: Line 9 Wscript.Sleep 1500. This command makes sure that the script waits for notepad to open before continuing with the SendKeys commands. Note 2: Spot the { } braces for special characters, for example carriage return {ENTER} or tab {TAB}. The % sign is the equivalent of pressing the alt key and is used to invoke the File menu. Note 3: I challenge you to experiment with the command, 'Do While... loop. If you want a bigger challenge substitute, For... Next. Note 4: Can you see what variable Racey is doing? Did you notice that each line is written progressively faster? Note 5: objShell.Run calls "Notepad". If you prefer, you could add the full path to the executable, for example, %SystemRoot%\system32\notepad.exe. However, when ever programs have been properly installed, I just use their executable name. Perhaps you can see that this is VBScripts equivalent of opening the Run Box on the start menu. N.B remember my warning: be careful if you have any documents open, there is a small chance that sendkeys could start writing in your file by mistake. Guy Recommends:
The Free IP Address Tracker (IPAT)
|
|||||
Custom Search
|
Guy Recommends: WMI Monitor and It's Free!
|
|
Home Copyright © 1999-2012 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |