我尝试用我已经制作的 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
您可以将代码直接嵌入到批处理脚本中,而无需使用临时文件。这将提高脚本的速度,因为不会有多余的 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%')))"
只需将您的回声包含在重定向到文件的(代码块)中即可。
这样代码就更容易阅读了。
为了不提前结束代码块,
里面的右括号必须用插入符号转义
^)
( 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
在我看来,一个“真实”混合体仅由一个文件组成。
这也是可能的,但需要通过命令行参数处理参数。从第十二行开始`>`需要变成`>>`。
>
字符会覆盖文件的内容并添加指定的内容,因此您最终只会得到最后一行。而 >>
字符将在指定行上添加到文件内容的末尾。您还需要将
msg
和 vce
变量括在引号中。
固定脚本:
@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
我尝试 shell32.dll 和 ShellExecuteA 来运行 tts.vbs 文件,它工作了一周,现在它不再工作了,我不知道为什么我使用 Windows 11 请帮助我:(