Pages

Saturday, 5 March 2016

Make the computer speak


Here is a GUI program which will speak out the text entered into the textbox or speak out the contents of a specified text file. This simple HTML application or hta file coded in VBScript has a stop button to stop the speaking once it has started. Sadly there is no function to pause the speech.


Paste the following code into notepad and save as a .vbs file.


<html>
<head>
<title>SpeakIt</title>
<HTA:APPLICATION
  APPLICATIONNAME="SpeakIt"
  ID="SpeakIt"
  VERSION="1.0"
  MAXIMIZEBUTTON="no"
  SCROLL="no"/>
</head>

<script language="VBScript">

Sub Window_OnLoad
Dim width,height
width=460
height=470
self.ResizeTo width,height
End Sub

Function PInt()
Set WshShell = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
If (boxa.value = "") Then
X = MsgBox("Enter the path for the text file to speak!", 48, "ERROR!")
Else If Not (fs.FileExists(boxa.value)) Then
X = MsgBox("The specified file does not exist!", 48, "ERROR!")
Else
path = WshShell.SpecialFolders("Templates")
Set a = fs.CreateTextFile(path & "\Script.vbs")
a.WriteLine("Set fs = CreateObject(" & Chr(34) & "Scripting.FileSystemObject" & Chr(34) & ")")
a.WriteLine("Set WshShell = CreateObject(" & Chr(34) & "WScript.Shell" & Chr(34) & ")")
a.WriteLine("path = WshShell.SpecialFolders(" & Chr(34) & "Templates" & Chr(34) & ")")
a.WriteLine("Set sapi = CreateObject(" & Chr(34) & "sapi.spvoice" &Chr(34) & ")")
a.WriteLine("Set a = fs.OpenTextFile(" & Chr(34) & boxa.value & Chr(34) & ")")
a.WriteLine("text = a.ReadAll")
a.WriteLine("sapi.Speak text")
a.Close
X = SetTimeOut("Speak()", 500)
End If
End If
End Function

Function Intialize()
If (box.value = "") Then
X = MsgBox("Enter the text to speak!", 48, "ERROR!")
Else
Set WshShell = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
path = WshShell.SpecialFolders("Templates")
Set a = fs.CreateTextFile(path & "\Script.vbs")
Set b = fs.CreateTextFile(path & "\XFile.txt")
b.Write(box.value)
a.WriteLine("Set fs = CreateObject(" & Chr(34) & "Scripting.FileSystemObject" & Chr(34) & ")")
a.WriteLine("Set WshShell = CreateObject(" & Chr(34) & "WScript.Shell" & Chr(34) & ")")
a.WriteLine("path = WshShell.SpecialFolders(" & Chr(34) & "Templates" & Chr(34) & ")")
a.WriteLine("Set sapi = CreateObject(" & Chr(34) & "sapi.spvoice" &Chr(34) & ")")
a.WriteLine("Set a = fs.OpenTextFile(path " & Chr(38) & " " & Chr(34) & "\XFile.txt" & Chr(34) & ")")
a.WriteLine("text = a.ReadAll")
a.WriteLine("sapi.Speak text")
a.Close
b.Close
X = SetTimeOut("Speak()", 500)
End If
End Function

Function Speak()
Set WshShell = CreateObject("WScript.Shell")
path = WshShell.SpecialFolders("Templates")
X = WshShell.Run(path & "\Script.vbs")
End Function

Function StopSpeaking()
Set WshShell = CreateObject("WScript.Shell")
X = WshShell.Run("taskkill /im wscript.exe /f", 7)
End Function

</script>

<body bgcolor="white">
<table align="center">
<caption><hr><b>SpeakIt</b><hr></caption>
<tr>
<td>Enter the text file path to speak:</td>
</tr>
<tr>
<td align="center"><input type="text" id="boxa" size="65"></td>
</tr>
<tr>
<td align="center"><input id="BTNA" type="button" value="Speak it!" onclick="PInt()" style="width:420px;height:30px;"></td>
</tr>
<tr>
<td>Enter the text to speak:</td>
</tr>
<tr>
<td align="center"><textarea cols="50" rows="10" id="box"></textarea></td>
</tr>
<tr>
<td align="center"><input id="BTN" type="button" value="Speak it!" onclick="Intialize()" style="width:420px;height:30px;"></td>
</tr>
<tr>
<td align="center"><input id="BTNS" type="button" value="Stop Speaking" onclick="StopSpeaking()" style="width:420px;height:30px;"></td>
</tr>
</table>
</body>
</html>

Done.! Enjoy.!


No comments:

Post a Comment