You know how, in those science fiction movies, the computer always talks to its user? Like, literally? Well, a close approximation of this is possible if you're using Windows and you know VBScript. You need to write the script and save it in the correct directory.
Today, I'm going to show just how.
Create a text file and save it with the extension of ".vbs". For the moment, save it anywhere you like. Double-clicking on it should execute it, if you're on a Windows computer. If not, tough shit.
First, declare a variable named speech. Then assign something pronounceable for the computer to speak.
Dim speech
speech="Hello world"
speech="Hello world"
Next, create an object VObj by calling the CreateObject() function, passing in the argument "sapi.spvoice".
That ensures that the object you create is a voice activated object.
Dim speech
speech="Hello world"
Set VObj=CreateObject("sapi.spvoice")
speech="Hello world"
Set VObj=CreateObject("sapi.spvoice")
Finally, use this object to run the Speak() method, and pass in the variable speech. You hear that? Depending on your settings and Windows version, you should be hearing either a deep male voice or a (equally deep) female voice.
Dim speech
speech="Hello world"
Set VObj=CreateObject("sapi.spvoice")
VObj.Speak speech
speech="Hello world"
Set VObj=CreateObject("sapi.spvoice")
VObj.Speak speech
But you know what? "Hello world" is boring. Let's add a bit of zing to it. Let's make the voice greet you according to the time of the day.
Declare a second and third variable, greeting and currenthour.
Dim speech,greeting
Dim currenthour
speech="Hello world"
Set VObj=CreateObject("sapi.spvoice")
VObj.Speak speech
Dim currenthour
speech="Hello world"
Set VObj=CreateObject("sapi.spvoice")
VObj.Speak speech
Set the variable currenthour to the, well, current hour, using the Hour() function, passing in the Now() function as an argument. You'll get a number from 0 to 23.
Dim speech,greeting
Dim currenthour
currenthour=Hour(Now())
speech="Hello world"
Set VObj=CreateObject("sapi.spvoice")
VObj.Speak speech
Dim currenthour
currenthour=Hour(Now())
speech="Hello world"
Set VObj=CreateObject("sapi.spvoice")
VObj.Speak speech
Now, using multiple If conditionals, set the greeting variable according to the time of the day.
Dim speech,greeting
Dim currenthour
currenthour=Hour(Now())
if currenthour<=12 then
greeting="Good morning, "
end if
if currenthour>12 and currenthour <=18 then
greeting="Good afternoon, "
end if
if currenthour>18 then
greeting="Good evening, "
end if
speech="Hello world"
Set VObj=CreateObject("sapi.spvoice")
VObj.Speak speech
Dim currenthour
currenthour=Hour(Now())
if currenthour<=12 then
greeting="Good morning, "
end if
if currenthour>12 and currenthour <=18 then
greeting="Good afternoon, "
end if
if currenthour>18 then
greeting="Good evening, "
end if
speech="Hello world"
Set VObj=CreateObject("sapi.spvoice")
VObj.Speak speech
Next, concatenate the greeting variable to your name, and set it to the variable speech. Run your code. Does the computer give you a nice greeting?
Dim speech,greeting
Dim currenthour
currenthour=Hour(Now())
if currenthour<=12 then
greeting="Good morning, "
end if
if currenthour>12 and currenthour <=18 then
greeting="Good afternoon, "
end if
if currenthour>18 then
greeting="Good evening, "
end if
speech=greeting & ", Teochew Thunder."
Set VObj=CreateObject("sapi.spvoice")
VObj.Speak speech
Dim currenthour
currenthour=Hour(Now())
if currenthour<=12 then
greeting="Good morning, "
end if
if currenthour>12 and currenthour <=18 then
greeting="Good afternoon, "
end if
if currenthour>18 then
greeting="Good evening, "
end if
speech=greeting & ", Teochew Thunder."
Set VObj=CreateObject("sapi.spvoice")
VObj.Speak speech
Let's make things even more interesting. Make the computer say random things according to the time of the day. For that, we'll create a randomizer function, and define more variables - max, min, randomno and randomstatement. Set max to 3 and min to 1. That's because we're only going to have three random statements for each time period.
Dim speech,greeting,randomstatement
Dim currenthour
Dim max,min
Dim randomno
max=3
min=1
Function getrandomno(min,max)
Randomize
getrandomno=(Int((max-min+1)*Rnd+min))
End Function
currenthour=Hour(Now())
if currenthour<=12 then
greeting="Good morning, "
end if
if currenthour>12 and currenthour <=18 then
greeting="Good afternoon, "
end if
if currenthour>18 then
greeting="Good evening, "
end if
speech=greeting & ", Teochew Thunder."
Set VObj=CreateObject("sapi.spvoice")
VObj.Speak speech
Dim currenthour
Dim max,min
Dim randomno
max=3
min=1
Function getrandomno(min,max)
Randomize
getrandomno=(Int((max-min+1)*Rnd+min))
End Function
currenthour=Hour(Now())
if currenthour<=12 then
greeting="Good morning, "
end if
if currenthour>12 and currenthour <=18 then
greeting="Good afternoon, "
end if
if currenthour>18 then
greeting="Good evening, "
end if
speech=greeting & ", Teochew Thunder."
Set VObj=CreateObject("sapi.spvoice")
VObj.Speak speech
Now, we're going to set randomno to a rand0m number generated by the function getrandomno(), then set the randomstatement variable to the result, using a Select Case block. I've set the different phrases, but feel free to make your own!
Dim speech,greeting,randomstatement
Dim currenthour
Dim max,min
Dim randomno
max=3
min=1
Function getrandomno(min,max)
Randomize
getrandomno=(Int((max-min+1)*Rnd+min))
End Function
currenthour=Hour(Now())
if currenthour<=12 then
randomno=getrandomno(min,max)
Select Case randomno
Case 1
randomstatement="You're looking sharp today."
Case 2
randomstatement="Rise and shine!"
Case Else
randomstatement="Had your coffee yet?"
End Select
greeting="Good morning, "
end if
if currenthour>12 and currenthour <=18 then
randomno=getrandomno(min,max)
Select Case randomno
Case 1
randomstatement="Hard at work, I see."
Case 2
randomstatement="Lovely weather!"
Case Else
randomstatement="I love your hair."
End Select
greeting="Good afternoon, "
end if
if currenthour>18 then
randomno=getrandomno(min,max)
Select Case randomno
Case 1
randomstatement="Isn't it a little late?"
Case 2
randomstatement="Time for bed!"
Case Else
randomstatement="The moon's out tonight."
End Select
greeting="Good evening, "
end if
speech=greeting & ", Teochew Thunder."
Set VObj=CreateObject("sapi.spvoice")
VObj.Speak speech
Dim currenthour
Dim max,min
Dim randomno
max=3
min=1
Function getrandomno(min,max)
Randomize
getrandomno=(Int((max-min+1)*Rnd+min))
End Function
currenthour=Hour(Now())
if currenthour<=12 then
randomno=getrandomno(min,max)
Select Case randomno
Case 1
randomstatement="You're looking sharp today."
Case 2
randomstatement="Rise and shine!"
Case Else
randomstatement="Had your coffee yet?"
End Select
greeting="Good morning, "
end if
if currenthour>12 and currenthour <=18 then
randomno=getrandomno(min,max)
Select Case randomno
Case 1
randomstatement="Hard at work, I see."
Case 2
randomstatement="Lovely weather!"
Case Else
randomstatement="I love your hair."
End Select
greeting="Good afternoon, "
end if
if currenthour>18 then
randomno=getrandomno(min,max)
Select Case randomno
Case 1
randomstatement="Isn't it a little late?"
Case 2
randomstatement="Time for bed!"
Case Else
randomstatement="The moon's out tonight."
End Select
greeting="Good evening, "
end if
speech=greeting & ", Teochew Thunder."
Set VObj=CreateObject("sapi.spvoice")
VObj.Speak speech
Next, concatenate randomstatement to speech. And run your code. Multiple times. Does it say something different each time?
Dim speech,greeting,randomstatement
Dim currenthour
Dim max,min
Dim randomno
max=3
min=1
Function getrandomno(min,max)
Randomize
getrandomno=(Int((max-min+1)*Rnd+min))
End Function
currenthour=Hour(Now())
if currenthour<=12 then
randomno=getrandomno(min,max)
Select Case randomno
Case 1
randomstatement="You're looking sharp today."
Case 2
randomstatement="Rise and shine!"
Case Else
randomstatement="Had your coffee yet?"
End Select
greeting="Good morning, "
end if
if currenthour>12 and currenthour <=18 then
randomno=getrandomno(min,max)
Select Case randomno
Case 1
randomstatement="Hard at work, I see."
Case 2
randomstatement="Lovely weather!"
Case Else
randomstatement="I love your hair."
End Select
greeting="Good afternoon, "
end if
if currenthour>18 then
randomno=getrandomno(min,max)
Select Case randomno
Case 1
randomstatement="Isn't it a little late?"
Case 2
randomstatement="Time for bed!"
Case Else
randomstatement="The moon's out tonight."
End Select
greeting="Good evening, "
end if
speech=greeting & ", Teochew Thunder. " & randomstatement
Set VObj=CreateObject("sapi.spvoice")
VObj.Speak speech
Dim currenthour
Dim max,min
Dim randomno
max=3
min=1
Function getrandomno(min,max)
Randomize
getrandomno=(Int((max-min+1)*Rnd+min))
End Function
currenthour=Hour(Now())
if currenthour<=12 then
randomno=getrandomno(min,max)
Select Case randomno
Case 1
randomstatement="You're looking sharp today."
Case 2
randomstatement="Rise and shine!"
Case Else
randomstatement="Had your coffee yet?"
End Select
greeting="Good morning, "
end if
if currenthour>12 and currenthour <=18 then
randomno=getrandomno(min,max)
Select Case randomno
Case 1
randomstatement="Hard at work, I see."
Case 2
randomstatement="Lovely weather!"
Case Else
randomstatement="I love your hair."
End Select
greeting="Good afternoon, "
end if
if currenthour>18 then
randomno=getrandomno(min,max)
Select Case randomno
Case 1
randomstatement="Isn't it a little late?"
Case 2
randomstatement="Time for bed!"
Case Else
randomstatement="The moon's out tonight."
End Select
greeting="Good evening, "
end if
speech=greeting & ", Teochew Thunder. " & randomstatement
Set VObj=CreateObject("sapi.spvoice")
VObj.Speak speech
OK, enough playing. Let's set it so that your computer greets you every time it starts up. Save this file to the C:\Users\[your user name here]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup directory. I'm currently using Windows 8.1 on this machine, and the startup folder may be somewhere else depending on your Windows version.
Once that's done, restart your computer and stand by!
Doesn't this code just speak to you?
T___T
T___T
No comments:
Post a Comment