Wednesday 2 July 2014

Make Your Windows Computer Talk like JARVIS

Set objFSO = CreateObject("Scripting.FileSystemObject")
strAFile = Wscript.Arguments(0)
Set objFile = objFSO.GetFile(strAFile)
If objFile.Size > 0 Then
Set objReadFile = objFSO.OpenTextFile(Wscript.Arguments(0), 1)
strContents = objReadFile.ReadAll
objReadFile.Close
set ObjVoice=CreateObject("SAPI.SpVoice")
ObjVoice.Speak strContents
Else
Wscript.Echo "The file is empty."
End If
The script above can be called from a command prompt using a txt files as the first parameter. You may not know how to pass parameters via the command prompt, so we’ll quickly go through this. If you’re familiar with the Windows batch, skip this
1. Create a text file called speak.vbs – for example
2. When saving the file, MAKE SURE that you enter .vbs as the file extension AND select All Files from the dropdown
3. Copy and paste the code snippet from above into the text file and hit CTRL + S to save the script
4. Alright, now create another text file (.txt file extension) and insert some random text like Today the world is going to end
5. Next, we’re going to test that everything works properly. Open a command prompt and cd into the directory where you stored the VBS script for example cd C:\snippets. You may have to change the drive letter to switch the drive. Simply enter C: or D: and hit enter to switch to another driver, then enter your “cd command”
6. If you followed all steps 1-5 correctly, you should now hear a voice speaking the text that is stored in your text file
7. If you don’t hear anything, double-check that your .vbs file has been created correctly and has the little VBS icon and the type should be “VBScript Script File”

No comments:

Post a Comment