1

I tried to make a hybrid Batch-VBS script out of a VBS script that i already made. It would give a inputBox, and use the results to sapi.spvoice.Speak it. I tried to make it into a batch script (below), but it doesn't work, and tts.vbs ends up containing only sapi.Speak message.


Batch Script:

@echo off
:start
cls
echo Batch Text-To-Speech
echo By SudDaBuilder
:: echo Fixed by %YourNameHere% ::
set /p msg=What do you want your PC to say? 
set /p vce=Choose a Voice (0 - Male, 1 - Female) 
pause
cls
echo Dim message, sapi, voice > tts.vbs
echo message=%msg% > tts.vbs
echo voice=%vce% > tts.vbs
echo Set sapi=CreateObject("sapi.spvoice") > tts.vbs
echo with sapi > tts.vbs
echo Set .voice = .getvoices.item(voice) > tts.vbs
echo .Volume = 100 > tts.vbs
echo end with > tts.vbs
echo sapi.Speak message > tts.vbs
cscript tts.vbs
cls
pause

:again
cls
set /p retry=Again? (y/n) 
if %retry% == y goto start
goto end

:end
echo See you soon!
ping localhost > nul
SudDaBuilder
  • 33
  • 1
  • 6

3 Answers3

3

Simply enclose your echos in a (code block) that is redirected to a file.
The code is much easier to read then.
To not end the code block prematurely,
the closing parentheses inside have to be escaped with a caret ^)

( echo Dim message, sapi, voice
  echo message=%msg%
  echo voice=%vce%
  echo Set sapi=CreateObject("sapi.spvoice"^)
  echo with sapi
  echo Set .voice = .getvoices.item(voice^)
  echo .Volume = 100
  echo end with
  echo sapi.Speak message
)  > tts.vbs

A real hybrid consists IMO of only one file. This is also possible, but requires handling of parameters via cmd line arguments.

3

You can embed the code directly into the batch script without using a temp file. This will increase the speed of the script as there will be no redundant IO operations:

<!-- : BATCH
    @echo off
    :start
    cls
    echo Batch Text-To-Speech
    echo By SudDaBuilder
    :: echo Fixed by %YourNameHere% ::
    set /p msg=What do you want your PC to say? 
    set /p vce=Choose a Voice (0 - Male, 1 - Female) 

    pause

    cscript //nologo "%~f0?.wsf" %msg% %vce%

    :again
    cls
    set /p retry=Again? (y/n) 
    if %retry% == y goto start
    goto end

    :end
    echo See you soon!
    ping localhost > nul
    exit /b %errorlevel%



BATCH : --->

<job><script language="VBScript">

Dim message, voice

message=WScript.Arguments.Item(0)
voice=WScript.Arguments.Item(1)

'WScript.Echo(voice & "--" & message) 

set sapi = CreateObject("SAPI.SpVoice") 
with sapi 
 Set .voice = .getvoices.item(voice) 
  '.Volume = 100
end with

sapi.Speak( message)

 </script></job>

You even can use the sp voice objects in a one line:

@echo off

set /p "to_say=enter a text :"

mshta "javascript:code(close((V=(v=new ActiveXObject('SAPI.SpVoice')).GetVoices()).count&&v.Speak('%to_say%')))"
npocmaka
  • 51,748
  • 17
  • 123
  • 166
2

The `>` needs to become `>>` from the twelfth line onwards.

Why?
The > character overwrites the contents of the file and adds the specifie content, so you only end up getting the last line. Whereas, the >> character(s) adds on the specified line to the end of the file's contents.
You also need to enclose the msg and vce variables in quotations.


Fixed script:

@echo off
:start
cls
echo Batch Text-To-Speech
echo By SudDaBuilder
:: echo Fixed by SO Suda ::
set /p msg=What do you want your PC to say? 
set /p vce=Choose a Voice (0 - Male, 1 - Female) 
pause
cls
echo Dim message, sapi, voice > tts.vbs
:: THIS IS THE TWELFTH LINE ::
echo message="%msg%" >> tts.vbs
echo voice="%vce%" >> tts.vbs
echo Set sapi=CreateObject("sapi.spvoice") >> tts.vbs
echo with sapi >> tts.vbs
echo Set .voice = .getvoices.item(voice) >> tts.vbs
echo .Volume = 100 >> tts.vbs
echo end with >> tts.vbs
echo sapi.Speak message >> tts.vbs
cscript //NoLogo tts.vbs
cls
pause

:again
cls
:: ADDED A DELETE FOR THE tts.vbs FILE::
del tts.vbs
set /p retry=Again? (y/n) 
if %retry% == y goto start
goto end

:end
echo See you soon!
ping localhost >> nul
Suda
  • 314
  • 4
  • 18