Revive that PC speaker with James Bond18 Jul 2008
Once upon a time the humble computer was not the multimedia hub that it is today. If you wanted a sound then you had to use the internal speaker that could just make a beeping noise. Modern PCs still contain such a speaker -its useful for giving warnings on the POST (Power On Self Test) part of the boot.
So why do I bring this up? Well I use VBScript (VBS) for a lot of my scripting on Windows and I often code scripts which carry out an operation and I like to know when they finish but having to click OK at the end I find a little bit of an annoyance. So instead I sometimes use the PC speaker to give me an audible cue that doesn’t require any further interaction from me.
Function Beep (p_intBeeps) 'Beep the PC speaker a number of times equal to the p_intBeeps parameter Dim objShell, intCount, strCommand 'Check we have a valid number of beeps 'not fool proof but it will do for now Beep = False if isnumeric(p_intBeeps) then if p_intBeeps > 0 then Beep = True end if end if if Beep Then 'Build the command string to run strCommand = "cmd /c echo" for intCount = 1 to p_intBeeps strCommand = strCommand & " " & chr(007) next 'Run the command in the background set objShell = Wscript.CreateObject("wscript.Shell") objShell.Run strCommand, 0 Beep = True end if End Function
Using this function it is quite simple to just call it using something like…
… to get two successive beeps in your VBS code. Hey presto you can now beep to your heart’s content.
The script is quite straight forward.
First of all it takes the number of beeps to produce as a parameter.
This is used in a loop to build up a command which can then be ‘run’.
cmd in the run dialog box in windows will open a new
instance of the windows command interpreter. Passing a parameter of
/c tells it that what follows as a parameter is the command to
The way in which the speaker is activated is by sending an BEL code to the standard output of a command prompt. This code has a value in ASCII of ‘007’. The script therefore uses this to build up a command that will send the 007 character to the standard output … which is what the echo command does from Windows’ command prompt.
The final step of the script is to get the CScript engine (for WSH) to run the command we have built up.