user3093536
user3093536

Reputation: 95

Batch-VBScript Hybrid Speech Script

This is my batch script:

@echo off
title 

setlocal enabledelayedexpansion
:a
set /p a=
!d!
for %%G in (%a%) do (set /a b+=1
if !b! neq 1 (set c=!c!-%%G) else (set c=%%G))
echo wscript.createobject("sapi.spvoice").speak "!c!">a.vbs
start a.vbs

exit

For every time that this program runs, it overwrites the a.vbs file with the new code as variable c. Is it possible to have "wscript.createobject("sapi.spvoice").speak "!c!"" preexisting in a VBScript and simply have batch assign the variable and execute it instead of overwriting and then executing?

With the help of aphoria, I tweaked my scripts to this: VBScript:

wscript.createobject("sapi.spvoice").speak wscript.arguments(0)

Batch Script:

@echo off
title 

setlocal enabledelayedexpansion
set /p a=
for %%G in (!a!) do (set /a b+=1
if !b! neq 1 (set c=!c!-%%G) else (set c=%%G))
cscript //nologo b.vbs !c!

exit

Upvotes: 1

Views: 897

Answers (4)

Mohd Asnawi
Mohd Asnawi

Reputation: 1

This is my batch script:

@echo off title setlocal enabledelayedexpansion :a set /p a= !d! 
for %%G in (%a%) 
  do (set /a b+=1 
     if !b! neq 1 
         (set c=!c!-%%G) 
    else 
        (set c=%%G)) 
    echo wscript.createobject("sapi.spvoice").speak "!c!">a.vbs 
    start a.vbs 
exit

Upvotes: 0

zask
zask

Reputation: 181

You dont have use a hybrid as you can embed the vbs file inside the batch file.

@echo off
set /p "Voice= Enter what you would like to say : "
echo dim speechobject >> sapi.vbs
echo set speechobject=createobject("sapi.spvoice") >> sapi.vbs
echo speechobject.speak "%Voice%" >> sapi.vbs
start sapi.vbs
timeout /t 1 /nobreak
del sapi.vbs

Upvotes: -1

Aacini
Aacini

Reputation: 67216

May I suggest you another solution?

JScript language is similar to VBScript, but have an advantage in this case: the JScript code can be placed inside the Batch file itself via a very simple trick. This way, it is not necessary to create a separated file with the JScript code:

@if (@CodeSection == @Batch) @then

:: Previous line is:
:: - in Batch: a valid IF command that does nothing
:: - in JScript: a conditional compilation IF statement that is false
::   so the following code is omitted until the next atSign-end

@echo off
title 

setlocal enabledelayedexpansion
set /p a=
for %%G in (!a!) do (set /a b+=1
if !b! neq 1 (set c=!c!-%%G) else (set c=%%G))
rem Execute this Batch file as a JScript one:
cscript //nologo //E:JScript "%~F0" !c!

exit

@end

WScript.CreateObject("sapi.spvoice").Speak(WScript.Arguments(0));

In this case the original VBScript code is so simple that the JScript translation is immediate; just note that the uppercase letters are needed in JScript. However, I am not entirely sure that spvoice execute the same in JScript than in VBScript; you must do a test...

Upvotes: 1

aphoria
aphoria

Reputation: 20189

Create a script file SpeakNumber.vbs (call it whatever you want).

Put this inside it:

Set args = Wscript.Arguments

WScript.CreateObject("sapi.spvoice").Speak args(0)

Then, change your batch file like this:

@echo off
title 

setlocal enabledelayedexpansion
:a
set /p a=
!d!
for %%G in (%a%) do (set /a b+=1
if !b! neq 1 (set c=!c!-%%G) else (set c=%%G))
START SpeakNumber.vbs !c!

exit

Upvotes: 1

Related Questions