Making your computer talk with VBScript.

Who doesn’t enjoy making their computer talk? Here are some fun scripts to turn your computer into a chatterbox.

This first one tells the time.

' speaktime.vbs
Set objVoice = CreateObject("SAPI.SpVoice")
objVoice.Speak "The time is " & Hour(Now) & " " & Minute(Now)

A variation of this script is one that vaguely tells you the time. I got the inspiration for this script from a program called FuzzyClock that does the same thing, albeit quietly.

' fuzzyclock.vbs
Dim strHour, strMin, FuzzHour, FuzzMin, NextHour
FuzzTime = Now
StrHour = Hour(FuzzTime)
StrMin = Minute(FuzzTime)

if StrMin = 0 then
FuzzMin = "exactly "
elseif StrMin > 0 and StrMin < 5 then
FuzzMin = "just after "
elseif StrMin >= 5 and StrMin < 10 then
FuzzMin = "well past five after "
elseif StrMin >= 10 and StrMin < 15 then
FuzzMin = "nearly a quarter after "
elseif StrMin = 15 then
FuzzMin = "a quarter after "
elseif StrMin >= 15 and StrMin < 20 then
FuzzMin = "just past a quarter after "
elseif StrMin >= 20 and StrMin < 25 then
FuzzMin = "well past a quarter after "
elseif StrMin >= 25 and StrMin < 30 then
FuzzMin = "nearly half past "
elseif StrMin = 30 then
FuzzMin = "half past "
elseif StrMin >= 30 and StrMin < 35 then
FuzzMin = "just half past "
elseif StrMin >= 35 and StrMin < 40 then
FuzzMin = "well gone half past "
elseif StrMin >= 40 and StrMin < 45 then
FuzzMin = "nearly a quarter to "
StrHour = 1 + StrHour
elseif StrMin = 45 then
FuzzMin = "a quarter to "
StrHour = 1 + StrHour
elseif StrMin >= 45 and StrMin < 50 then
FuzzMin = "past a quarter to "
StrHour = 1 + StrHour
elseif StrMin >= 50 and StrMin < 55 then
FuzzMin = "well past a quarter to "
StrHour = 1 + StrHour
elseif StrMin >= 55 then
FuzzMin = "almost "
StrHour = 1 + StrHour
end if

if strHour = 13 or strHour = 01 then 
FuzzHour = "one"
elseif strHour = 2 or strHour = 14 then 
FuzzHour = "two"
elseif strHour = 3 or strHour = 15 then 
FuzzHour = "three"
elseif strHour = 4 or strHour = 16 then
FuzzHour = "four"
elseif strHour = 5 or strHour = 17 then 
FuzzHour = "five"
elseif strHour = 6 or strHour = 18 then
FuzzHour = "six"
elseif strHour = 7 or strHour = 19 then 
FuzzHour = "seven"
elseif strHour = 8 or strHour = 20 then
FuzzHour = "eight"
elseif strHour = 9 or strHour = 21 then 
FuzzHour = "nine"
elseif strHour = 10 or strHour = 22 then
FuzzHour = "ten"
elseif strHour = 11 or strHour = 23 then
FuzzHour = "eleven"
elseif strHour = 12 or strHour = 00 then 
FuzzHour = "twelve"
end if

wscript.echo  "It's " & FuzzMin & FuzzHour & "."

This next one tells you the day of the week, along with some added commentary.

' speakweek.vbs
Set objVoice = CreateObject("SAPI.SpVoice")

dtmToday = Date()

dtmDayOfWeek = DatePart("w", dtmToday)

Select Case dtmDayOfWeek
    Case 1 objVoice.Speak "Working on a SUNDAY? Something's not right here."
    Case 2 objVoice.Speak "Today's Monday. Blargh."
    Case 3 objVoice.Speak "Today's Tuesday. Leave me alone."
    Case 4 objVoice.Speak "It's Wednesday. At least the week's half over."
    Case 5 objVoice.Speak "Today's Thursday. The week's almost over."
    Case 6 objVoice.Speak "It's FRIDAY! WOOT!"
    Case 7 objVoice.Speak "What are you doing here on Saturday? Take the day off!"
End Select

Finally, here’s a script that prompts you for some text you want your computer to recite.

' speaker.vbs
strText = InputBox("What do you want me to say?", _
Set objVoice = CreateObject("SAPI.SpVoice")
objVoice.Speak strText

Have much fun.

A talking number guessing game.

Here’s a surprisingly simple VBScript that plays a number guessing game. It uses your computer’s voice to tell you whether your guess is too high or too low.

Set objVoice = CreateObject(“SAPI.SpVoice”)

intHighNumber = 100
intLowNumber = 1

intNumber = Int((intHighNumber – intLowNumber + 1) * Rnd + intLowNumber)

objVoice.Speak “Guess the number!”

Do Until Cint(StrGuess)=IntNumber

strGuess = InputBox(“Enter your guess.”, _
“Guess the Number”)
if strGuess=”” then
elseif Cint(strGuess) > IntNumber then
objVoice.Speak “Too high.”
elseif Cint(strGuess) < IntNumber then
objVoice.Speak “Too low.”
end if


objVoice.Speak “You guessed it!”

Generating cover letters with VBScript.

I’ve long been using AutoIt to generate the cover letters I send out when applying for jobs but today I decided to see if VBScript has the ability to simulate keystrokes as well. And it does. I found this article which introduces the SendKeys command for sending text and keystrokes. Minutes later, I had a VBScript version of my cover letter generator.

Option Explicit
Dim objShell, WshShell, strJob
set objShell = CreateObject(“WScript.Shell”)
strJob = InputBox(“Position applied for:”, _
WScript.Sleep 5000
objShell.SendKeys “Attn: Personnel Manager”
objShell.SendKeys “{Enter}”
objShell.SendKeys “Re: ” & strJob & ” Position”
objShell.SendKeys “{Enter}{Enter}”
objShell.SendKeys “My name is Joe Smith. I am an experienced Computer Technician, having worked in the IT Departments of Company1 and Company2. I am familiar with high-volume, deadline driven environments. You will find my experiences and aptitudes a match for the position described. Enclosed is my resume for consideration for the ” & strJob & ” position with your company. Please contact me at your earliest convenience, (xxx) xxx-xxx or by e-mail at to set up an interview. Thank you for your time and consideration.”
objShell.SendKeys “{Enter}”
objShell.SendKeys “{Enter}”
objShell.SendKeys “Best Regards,”
objShell.SendKeys “{Enter}”
objShell.SendKeys “{Enter}”
objShell.SendKeys “Joe Smith”
objShell.SendKeys “{Enter}”
objShell.SendKeys “Computer Technician Professional”

The script pauses for 5 seconds before sending out the keystrokes to give you time to click on the field where the text is to be sent.

JKDefrag is now MyDefrag.

JKDefrag, long one of my favorite disk defrag programs, is now MyDefrag. It’s still the same great utility that’s very stable, free and easy to use, but it now has a scripting engine to allow you to create scripts to run it exactly the way you want it. There are more to configure this time, from how long the defrag should run to whether or not for it to reboot when it’s finished. The scripting language is fairly simple to learn.
Although MyDefrag now requires installation, it will still run portably from your USB key drive. Just copy the program folder structure to your USB drive and delete the unins000.exe, unins000.dat and uninstall.exe files. I noted the various defrag methods in its start menu and made a series of VB Scripts to launch that method from my USB key drive.

Analyze Only

Set objShell = CreateObject(“Wscript.Shell”)
objShell.Run(“MyDefrag.exe -r AnalyzeOnly.MyD”), 1, TRUE

Defragment Only

Set objShell = CreateObject(“Wscript.Shell”)
objShell.Run(“MyDefrag.exe -r DefragmentOnly.MyD”), 1, TRUE

Fast Optimize

Set objShell = CreateObject(“Wscript.Shell”)
objShell.Run(“MyDefrag.exe -r FastOptimize.MyD”), 1, TRUE

Fast Update

Set objShell = CreateObject(“Wscript.Shell”)
objShell.Run(“MyDefrag.exe -r FastUpdate.MyD”), 1, TRUE

Flash Memory Disk Optimization

Set objShell = CreateObject(“Wscript.Shell”)
objShell.Run(“MyDefrag.exe -r FlashMemoryDisks.MyD”), 1, TRUE

Slow Optimize (Very thorough defragmentation)

Set objShell = CreateObject(“Wscript.Shell”)
objShell.Run(“MyDefrag.exe -r SlowOptimize.MyD”), 1, TRUE

In MyDefrag’s scripts folder is a file called Settings.MyD that stores the basic settings, and it is here that you configure the option for what should happen after the defrag is finished. By default the program window stays open, but you can change it so it automatically quits. Just change line 16 to:


I use MyDefrag at work as part of the monthly maintenance I do on the computers there. The last thing I do is a disk defrag as it often takes a long time to run, so it makes good sense to have the program automatically quit so the computer can reboot, thus allowing me to exit stage right, unnoticed.

Posting to Twitter with VBScript.

I still can’t believe how easy it is to post to Twitter using their Application Program Interface. All you need is the very handy cURL utility and you’re all set to start tweeting from the command prompt. I managed to take this one step further by creating a very simple Twitter client in VBScript.

strTweet = InputBox(“What are you doing?”, _
Set objShell = CreateObject(“Wscript.Shell”)
objShell.Run(“curl -u Your Twitter ID:Your Twitter password -d status=” & chr(34) & strTweet & chr(34) & ”;), 1, TRUE

Before running the script, replace the red text with your Twitter user ID and password. cURL does use basic authentication to log you on, so you should be safe.
When you run the script, an input box appears for your typing your tweet. I know there’s a way to make sure the tweet does not exceed 140 characters but for now I just wanted to make a very quick ‘n’ dirty Twitter client. After you hit ENTER, the script launches cURL to post your tweet on Twitter. It should appear online instantly.

Flushing the DNS cache from the login script.

At the office where I work are three servers. When a user logs onto the network, a login script runs and maps some network drives to shared folders across all three servers. However, there is one server on which the mapped drives don’t always appear. I came up with a fix for this problem but its effects seem temporary and wear off for reasons I’m still trying to fathom. What I do is drop to a command prompt and type:

ipconfig /flushdns

Afterwards I re-run the login script for that user and the missing drives magically re-appear.
To save even more time, I’ve added two lines at the beginning of the login script that flush the DNS cache before the network drives are mapped.

Set objShell = Wscript.CreateObject(“Wscript.Shell”)“%comspec% /c ipconfig /flushdns”)

When the script is run, a DOS window will briefly flash on the screen as the DNS cache is flushed, allowing me to continue investigating the drive mapping puzzle undisturbed.

Control Panel access through VBScript.

Using the information at this page about running Control Panel applets from the command prompt, I made a series of small VB scripts to make it easier to access them from my favorite file manager. This first script runs the Add/Remove Programs applet.

Set objShell = CreateObject(“Wscript.Shell”)
objShell.Run(“control appwiz.cpl”), 1, TRUE

And this one brings up your System properties window.

Set objShell = CreateObject(“Wscript.Shell”)
objShell.Run(“control sysdm.cpl”), 1, TRUE


Making all your apps portable.

Now that I have a USB key drive, I’ve really taken an interest in copying frequently used programs to it so I can run them wherever I go. Most of these programs save their settings to an INI file in the program’s folder, leaving nothing behind on the host computer. However, there are some programs I use that save their settings to the registry, not making them ideal for portable use. Until now. I came up with a way to make even these programs portable. Here’s how I did it.
As an example, one program I use is GIF Movie Gear, a tool perfect for making your own animations. After installing Movie Gear, I copy its program files to a new folder on my USB drive. Then I run it as usual from the start menu so I can enter my registration code and tweak the program settings to my liking. When I’m done, I close the program.
Then I go to the registry editor and go to HKEY_CURRENT_USER\Software and look for an entry for Movie Gear. Sure enough, it’s stored in a key called Gamani, which is the company that created Movie Gear. I export this key to a .reg file, which I copy to Movie Gear’s folder USB drive.
The final step is removing this key from the registry when you’re done using the program. I’ve experimented with deleting registry entries using VBScript but had a much easier time with AutoIt. Here’s the script:

RegDelete (“HKEY_CURRENT_USER\Software\gamani”)
MsgBox (4096, “GIF Movie Gear”, “Done!”)

I then create a VBScript that runs the above AutoIt script and save this script to the Movie Gear folder on my USB drive as cleanup.vbs.

Set objShell = CreateObject(“Wscript.Shell”)
objShell.Run(“I:\briefcase\AutoIt3\AutoIt3 I:\briefcase\scripts\autoit\moviegear.au3”), 1, TRUE

After all this is done, I then uninstall Movie Gear from the hard disk. It is now set up to run from the USB drive. From now on, whenever I need to use Movie Gear, I double-click on the .reg file I created earlier to import the program’s settings into the registry. Then I run Movie Gear as usual. When I’m done, I run the cleanup.vbs script to clear up Movie Gear’s tracks, leaving absolutely nothing behind on the host computer.