PowerShell Ezine, Logon Scripts

Guy's Scripting Ezine 26 - Run, SendKeys and Sleep

Contents for Guy's Scripting Ezine 26 - Run, SendKeys and Sleep

This week’s secret - Everything has a purpose

I 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

  1. Copy and paste the script below into notepad.
    Alternatively, get a trial copy of OnScript.
  2. Save the file with .vbs extension e.g. SendKeys.vbs.
  3. Double click and observe Notepad open.
  4. Be amazed to see a phantom typist.

 

 

' SendKeys.vbs
' Example VBScript Run Notepad and use SendKeys
' Author Guy Thomas http://computerperformance.co.uk/
' Ezine 26 Version 1.8 - June 2005
' -----------------------------------------------------'
Option Explicit
Dim objShell, Racey, intCount
Set objShell = CreateObject("WScript.Shell")
objShell.Run "notepad"
Wscript.Sleep 1500
Racey = 1000
intCount=0

Do While intCount < 7
objShell.SendKeys "Hello Guy"
objShell.SendKeys "{TAB}"
objShell.SendKeys "This is line: " & intCount
objShell.SendKeys "{ENTER}"
WScript.Sleep Racey
intCount = intCount + 1
Racey = Racey - 100
Loop
objShell.SendKeys "%F"
WScript.Sleep 1500
objShell.SendKeys "x"
WScript.Sleep 1500
objShell.SendKeys "{TAB}"
WScript.Sleep 500
objShell.SendKeys "{ENTER}"

WScript.Quit
' End of Example SendKeys VBScript

Learning Points

Note 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.

See Also Comspec

ˇ

Challenge:

Try other programs like Winword in place of Notepad.  It's also fun to open web pages with objShell.Run "http:xxxxx".  However, before you try this, remember my warning about SendKeys.  To begin with, remove all lines after the .Run command, which contain SendKeys.
This is an example of .send method, but with the HTTP variation of
objShell.Run: - "http://computerperformance.co.uk/"

 

' Web.vbs
' Example VBScript Run Notepad and use SendKeys
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.2 - April 18th 2004
' -----------------------------------------------------------------'
Option Explicit
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "http://computerperformance.co.uk/"
WScript.Quit

' End of Example VBScript .run method

Summary of SendKeys and .Run

This Example uses the WshShell.Run method.  In addition the instructions are designed to give you experience of Sleep, and SendKeys. All the while, you will be re-enforcing other VBScript techniques, for example looping.

Guy's Out Takes

What is the matter with 'Racey' in this script?

Answer = Value to high reduce from 400 to 140

 

' SendKeys.vbs
' Example VBScript Run Notepad and use SendKeys
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.9 - April 18th 2004
' -----------------------------------------------------------------'
Option Explicit
Dim objShell, Racey, Count
Set objShell = CreateObject("WScript.Shell")
objShell.Run "notepad"
Wscript.Sleep 1500
Racey = 1000
Count=0

Do While Count < 7
objShell.SendKeys "Hello Guy"
objShell.SendKeys "{TAB}"
objShell.SendKeys "The score is: " & Count
objShell.SendKeys "{ENTER}"
WScript.Sleep Racey
Count = Count + 1
Racey = Racey - 400
Loop

Wscript.Echo "How many Lines? " & count
WScript.Quit

' End of SendKeys VBScript Example

Computer Training Software - Recommended Training VideosGuy Thomas recommends Computer Training Software

Their topics and material are ideal for getting you started with VBScript.  The videos are easy to follow and you can control the pace.  Try their free demo material and then see if you want to buy the full package. See more about VB Script Training CD.


 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.