Hybrid Batch-VBS TTS脚本无法正常工作

时间:2018-06-02 07:48:35

标签: batch-file vbscript text-to-speech

我尝试用我已经制作的VBS脚本制作一个混合的Batch-VBS脚本。它会给出inputBox,并将结果用于sapi.spvoice.Speak。我试着把它变成一个批处理脚本(下面),但是它不起作用,并且tts.vbs最终只包含 sapi.Speak message

批处理脚本:

@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

3 个答案:

答案 0 :(得分:3)

只需将回声封装在重定向到文件的(代码块)中即可 那时代码更容易阅读。
不要过早地结束代码块,
内部的右括号必须使用脱字符^)

进行转义
( 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

真正的混合包含IMO只有一个文件。 这也是可能的,但需要通过cmd行参数处理参数。

答案 1 :(得分:3)

您可以使用临时文件嵌入code directly into the batch script without。这将提高脚本的速度,因为不会有冗余的IO操作:

<!-- : 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>

您甚至可以在一行中使用sp语音对象:

@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%')))"

答案 2 :(得分:2)

`&gt;`需要从第十二行起成为`&gt;&gt;`。

为什么?
>字符会覆盖文件的内容并添加特定内容,因此您最终只能获得最后一行。然而,>>字符在指定的行上添加到文件内容的末尾。
您还需要将msgvce变量括在引号中。

修正脚本:

@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