Verbal Alerts in Batch and other Windows Scripts

AID: 9631
  • Status: Published

1230 points

  • By
  • TypeTips/Tricks
  • Posted on2012-02-13 at 17:45:47
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly instantly when you only have a little to do and could take many minutes when you have a lot to do and short of paying close attention to the progress of the script (about as interesting as reading pi), you probably won't know when the script actually completes.  
 
To solve this problem, at least for myself, I wrote the following VBS script that verbally tells you when it's done.  This script, when saved to a .vbs file, should execute much like any other command line command.  Thus, it can be easily integrated into any batch script (really any scripting language that can execute a command line command) and with a little tweaking, incorporated into other VBS scripts.
Option Explicit

On Error Resume Next

Dim Speaker 'Becomes the object that is used to speak.
Dim VoiceList, Voice 'Used in the loop to list the available voices
Dim Words 'Words to speak
Dim Speed 'Speed to speak, range is -10 (slow) to 10 (fast)

'Create the speaker object
Set Speaker = CreateObject("SAPI.SpVoice")

'Get the words to speak
Words = WScript.Arguments.Item(0)

If Err.Number = 9 Then 'No command line parameters entered, including words to speak.  Popup a usage dialog

 Words="Nothing to say!" & vbCrLf & vbcrlf & "Syntax:" & vbCrLf & "SPEAK ""hello world"" [slow|normal|fast|-10 to 10]"
 MsgBox Words

Else

 If Lcase(WScript.Arguments.Item(0)) = "list" Then 'list the voices available - most systems only have the default Microsoft Ana.  

  For each Voice in Speaker.GetVoices
   VoiceList = VoiceList & Voice.GetDescription & vbcrlf
  Next

  MsgBox VoiceList

 Else 'speak the word or words in quotes specified as the first parameter

  Select Case LCASE(WScript.Arguments.Item(1)) 'If a second parameter is specified, it's the speed of the voice.  Talk slow or Talk fast.
   Case "slow"
    Speed = -5 'slower

   Case "fast"
    speed = 5 'faster

   case else 'either set the speech speed to normal OR use an absolute value (-10 to 10 - very slow to very fast)
    If IsNumeric(LCASE(WScript.Arguments.Item(1))) = True Then 'If the second parameter is a number then
     If CInt(WScript.Arguments.Item(1)) > -11 And CInt(LCASE(WScript.Arguments.Item(1))) < 11 Then 'checking that the number is between -10 and 10
      Speed = WScript.Arguments.Item(1)
     Else 'an invalid number was entered, using the standard speed.
      Speed = 0
     End If
    Else 'speed not specified, defaulting to normal
     Speed = 0
    End If
  End Select

  Speaker.Rate = speed 'set the speech speed for the speaker
  Speaker.Speak Words 'say the words as obtained earlier. 

 End If
End If
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:

Select allOpen in new window


Save the above code as "speak.vbs" somewhere that's in your path environment variable.
 
To test, create a small batch file that does nothing more than provide a directory listing:
@echo off
dir c: /s /b
speak "Directory listing complete"
                                    
1:
2:
3:

Select allOpen in new window



And then execute the batch file.
(Just make sure your sound is not muted and your speakers are on!)

TIP:
I create two folders on every system I setup - C:\Windows\Utils and C:\Windows\Scripts.  I then add these folders to my system's path statement.  This allows me to store all my scripts and utilities in appropriate places, separate from Windows files, and access them easily regardless of where my script needs to execute.  Otherwise, you would constantly need to reference the full path of the scripts you create or store them in places like C:\Windows\System32 - a less than ideal place.

 
If you prefer to use vbs scripts yourself the "magic line" (not specifically in the above script) with no alterations is:
 
CreateObject("SAPI.SpVoice").Speak "Hello World"
                                    
1:

Select allOpen in new window



And if you're familiar with using objects in VBS scripts, you'll likely easily decode my script and be able to a adapt it to whatever project you like.

It's also worth noting that there are many variants of VB and the syntax will likely be the same or similar if you want to incorporate a verbal alert in a macro for an Office file like an Excel spreadsheet or Word document - VBA and VBS are very similar.

One final note - if the words you're using aren't pronounced properly, consider spelling phonetically.
Asked On
2012-02-13 at 17:45:47ID9631
Tags

Batch

,

VBA

,

Visual Basic

,

VBS

,

WSH

,

Speech

,

Text-To-Speech

,

TTS

,

Script

Topic

Scripting Languages

Views
631

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top Scripting Languages Experts

  1. mplungjan

    145,789

    Master

    0 points yesterday

    Profile
    Rank: Savant
  2. Ray_Paseur

    91,292

    Master

    0 points yesterday

    Profile
    Rank: Savant
  3. billprew

    63,376

    Master

    0 points yesterday

    Profile
    Rank: Genius
  4. RobSampson

    54,636

    Master

    0 points yesterday

    Profile
    Rank: Genius
  5. DaveBaldwin

    51,700

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  6. sedgwick

    43,950

    1,600 points yesterday

    Profile
    Rank: Genius
  7. leakim971

    43,184

    0 points yesterday

    Profile
    Rank: Genius
  8. dgofman

    36,400

    0 points yesterday

    Profile
    Rank: Genius
  9. COBOLdinosaur

    28,424

    0 points yesterday

    Profile
    Rank: Genius
  10. woolmilkporc

    24,200

    0 points yesterday

    Profile
    Rank: Genius
  11. ozo

    20,600

    0 points yesterday

    Profile
    Rank: Savant
  12. Qlemo

    19,984

    2,000 points yesterday

    Profile
    Rank: Genius
  13. chaituu

    19,100

    0 points yesterday

    Profile
    Rank: Sage
  14. tagit

    19,000

    0 points yesterday

    Profile
    Rank: Genius
  15. farzanj

    18,550

    0 points yesterday

    Profile
    Rank: Genius
  16. nap0leon

    15,094

    0 points yesterday

    Profile
    Rank: Sage
  17. paultomasi

    14,700

    0 points yesterday

    Profile
    Rank: Master
  18. StingRaY

    13,136

    0 points yesterday

    Profile
    Rank: Wizard
  19. jason1178

    13,044

    0 points yesterday

    Profile
    Rank: Genius
  20. HainKurt

    12,350

    0 points yesterday

    Profile
    Rank: Genius
  21. HonorGod

    11,600

    0 points yesterday

    Profile
    Rank: Genius
  22. ahoffmann

    11,136

    0 points yesterday

    Profile
    Rank: Genius
  23. basicinstinct

    10,800

    0 points yesterday

    Profile
    Rank: Genius
  24. leew

    10,030

    0 points yesterday

    Profile
    Rank: Savant
  25. ChrisStanyon

    9,864

    0 points yesterday

    Profile
    Rank: Sage

Hall Of Fame