Guy's Scripting Ezine 89 - Simple but Elegant SendKeys
Contents for Ezine 89 - Simple but Elegant SendKeys
♣
This week's script illustrates the best of learning about VBScript on the internet. Jeremy H. wrote asking me for help with a scripting problem.
His scenario was that he wanted a VBScript which refreshed the desktop. He researched a script which is widely available on the internet (not on my site.) Unfortunately, the script did not do what
Jeremy wanted. I gave the script a quick look, but could not fix it. Then Jeremy himself, came up with an elegant solution that we are pleased to share with you.
Guy Recommends: Tools4ever's UMRA
Tired of writing scripts? The User Management Resource Administrator solution
by Tools4ever offers an alternative to time-consuming manual processes.
It
features 100% auto provisioning, Helpdesk Delegation, Connectors to more than
130 systems/applications, Workflow Management, Self Service and many other
benefits. Click on the link for more information on
UMRA.
Jeremy wanted a script to refresh the desktop. The scenario was that his users logged on just as a group policy finished delivering short cuts to the desktop. What confused the users was that the icons
only appeared after a refresh, such as pressing F5. Jeremy wanted a VBScript to tag on at the end of a logon script, which would refresh the desktop automatically.
Version 1 - Problem Won't Refresh the Desktop. 'Refresh the desktop set oShell= createobject("shell.application") set oDesktop = oShell.Namespace(0) oDesktop.self.invokeVerb "R&efresh"
Guy Learning Points
Note 1: I liked and admired 'self.invokeVerb', unfortunately in Jeremy's case, this method did not refresh the desktop. Nevertheless, I will add self.invokeVerb to my notebook of useful commands.
Calculating IP Address
ranges is a black art, which many network managers solve by creating custom
Excel spreadsheets. IPAT cracks this problem of allocating IP addresses
in networks in two ways:
For Mr Organized there is a nifty subnet
calculator, you enter the network address and the subnet mask, then IPAT
works out the usable addresses and their ranges.
For Mr Lazy IPAT
discovers and then displays the IP addresses of existing computers.
Download the Free IP Address Tracker
' Desktop.vbs - to Refresh Icons on the Desktop ' Example VBScript to re-activate the Desktop Shell ' Author Jeremy H. & Guy Thomas http://computerperformance.co.uk/ ' Version 2.1 -
October 2005 ' -------------------------------------------------------------' Option Explicit Dim WSHShell, strDesktop Set WSHShell = WScript.CreateObject("WScript.Shell") strDesktop =
WSHShell.SpecialFolders("Desktop") WSHShell.AppActivate strDesktop WSHShell.SendKeys "{F5}" WScript.Quit
' End of VBScript to refresh the desktop
Guy Learning Points Note 1: Where other scripts create a Network or an Active Directory object, so this script creates a shell
object. Jeremy declares his object and names it : WSHShell. In other scripts I call the same object objShell, it just goes to show that there numerous ways of creating and naming VBScript objects. Note 2: strDesktop =
WSHShell.SpecialFolders("Desktop"), here is the clever way that the script gets a handle on the desktop. In particular, note the central role of the property .SpecialFolders. Note 3:
WSHShell.SendKeys "{F5}". Pay close attention to the syntax for passing the famous function key five command to SendKeys.
In this version I added primitive error correcting code, combined with a confirmation message. I also
introduce a delay with WScript.Sleep.
' Desktop.vbs - to Refresh Icons on the Desktop ' Example VBScript with Sleep and err.number ' Author Jeremy H. & Guy Thomas http://computerperformance.co.uk/ ' Version 2.3 - October 2005
' -------------------------------------------------------------' Option Explicit Dim WSHShell, strDesktop Set WSHShell = WScript.CreateObject("WScript.Shell") strDesktop =
WSHShell.SpecialFolders("Desktop") WSHShell.AppActivate strDesktop WScript.Sleep 500 WSHShell.SendKeys "{F5}" If err.number = 0 then WScript.Echo "Desktop Refreshed "
Else WScript "Error " & err.number err.number = 0 End if WScript.Quit
' End of VBScript to refresh the desktop
Guy Learning Points Note 1: I could not resist adding a little routing to confirm that the script had run successfully. The key point is the If statement tests that
there is no error: If err.number = 0 Note 2: Scripts involving SendKeys often work fine on my machine but fail on other people's systems. To overcome this irritation, I add a WScript.Sleep
500 command, which tells VBScript to wait half a second before processing the next command. This Sleep technique is particularly suitable for logon scripts.
I just love scripts that are instructive at more than one level. The second script employs SendKeys to refresh the desktop. Where as the third script adds error-correcting code. Between them, the three scripts show a variety of ways to
approach a scripting problem. My hidden agenda is the power of the internet to research VBScript methods.
See more about VBScript
• VBScripts • Ezines • WMI • Logon Scripts
• Tool Kit
•
Ezine 26 SendKeys • Ezine 34 Scriptomatic • Ezine 47 .put •
Ezine 51 Sleep •
Ezine 52 OS
• Ezine 77 Scriptomatic •
Ezine 84 AcctInfo • Ezine 88 Progress bar • Ezine 89 SendKeys
• Ezine 97 Net Time • Ezine 98 W32 Time • Ezine
99 Time services • Ezine 120 Sendkeys
|