B4J Question Call Powershell from b4j

gezueb

Active Member
Licensed User
Longtime User
I try to call powershell with the parameters:
PowerShell -Command "Add-Type –AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak('hello');"
However, I cannot figure out how to do it.
B4X:
    shl.Initialize("shl", "java", _
         Array As String("Powershell -Command Add-Type –AssemblyName System.Speech;(New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak('Good Morning');"))
does nothing, not even throw an exception. The shl.process_completed is not raised. If the above command line however is entered in powershell, it works.
Thanks for help:)
 

Chris2

Active Member
Licensed User
Longtime User
I'm not sure about the arguments you're using there, or setting the command as "java", but you need to run the command after initialising:
B4X:
shl.Initialize("shl", "java", _
         Array As String("Powershell -Command Add-Type –AssemblyName System.Speech;(New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak('Good Morning');"))
shl.Run(-1)
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Thank you, Chris, I did not copy this statement in my first post, but I tried run / runsynchronous / runwithoutputevents. No luck. So maybe there is something wring with the parameters.
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
B4X:
Dim js As Shell
    Dim params As List
    params.Initialize
    Dim mystring As String=$"(Add-Type ....your command......)"$
    js.InitializeDoNotHandleQuotes("js", "powershell.exe", Array(mystring))
    js.WorkingDirectory="c:\windows\system32"   
    js.Run(-1)
    Wait for (js) js_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    'Log(StdOut)
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
The problem is obviously where you entered "your command" ! So I looked for a workaround and found one with Balabolka. It has a free download for a command line utility (balcon.exe) and is easy to call:

B4X:
Sub Button1_Click
    Private TextString As String = TextField1.text 'entered in the main form
    fx.Clipboard.SetString(TextString) 'get it to the clipboard
    shl.Initialize("shl","c:\program files\balcon\balcon.exe",Array As String("-c")) 'or any other reasonable path to balcon.exe
    shl.Run(10000) 'plenty of timeout
End Sub


Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    If Success And ExitCode = 0 Then
        Log("Success")
        Log(StdOut)
    Else
        Log("Error: " & StdErr)
    End If
End Sub

the -c command (there are a tad of others commands) speaks the contents of the text in the clipboard. Thanks to all for the help!
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
I also use Balcon for speech output.
Very nice
 
Upvote 0
Top