WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: stanl on February 09, 2021, 05:58:01 AM

Title: Morse Code url
Post by: stanl on February 09, 2021, 05:58:01 AM
Interesting url that translates text to Morse Code.  I know PS has a beep with parameters, but is there a way for WB to play back the code as beeps in a script.  below doesn't use Newtonsoft or Jim's Extender but the dots/dashes can be isolated into a list, array or map. Can they be either translated or played back in WB.
Code (WINBATCH) Select


;Winbatch 2020 - URL translates text to Morse Code
;Stan Littlefield 2/9/2021
;/////////////////////////////////////////////////////////////////////////////////////
gosub udfs
IntControl(73,1,0,0,0)
Msg = 'Winbatch 2021'
cUrl = "https://api.funtranslations.com/translate/morse.json?text=":Msg
request = Createobject("WinHttp.WinHttpRequest.5.1")
request.Open("GET", cUrl, @False )
request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
request.SetRequestHeader("Accept", "application/json")
request.Send()
jdata = request.ResponseText
request = 0
Message("Json Morse Code",parseit(GetJSON(jdata)))
Exit
;====================================================================================
:WBERRORHANDLER
request=0
geterror()
Message("Error Encountered",errmsg)
Exit


;====================================================================================
:udfs
#DefineSubRoutine geterror()
   wberroradditionalinfo = wberrorarray[6]
   lasterr = wberrorarray[0]
   handlerline = wberrorarray[1]
   textstring = wberrorarray[5]
   linenumber = wberrorarray[8]
   errmsg = "Error: ":lasterr:@LF:textstring:@LF:"Line (":linenumber:")":@LF:wberroradditionalinfo
   Return(errmsg)
#EndSubRoutine
#DefineFunction GetJSON(strJSON)
If strJSON == "" Then Return ""
objJSC = CreateObject ("MSScriptControl.ScriptControl")
objJSC.Language = "JScript"
objJSC.AddCode(: `function json2txt(obj,path){var txt='';for(var key in obj){if(obj.hasOwnProperty(key)){if('object'==typeof(obj[key])){txt+=json2txt(obj[key],path+(path?'|':'')+key);}else{txt+=path+'|'+key+','+obj[key]+'\n';}}}return txt;}`)
Return objJSC.Eval(: `json2txt(` : strJSON : `,'')`)
; JS code from Patrick Fisher at "http://stackoverflow.com/questions/10221229/list-all-keys-and-values-of-json" ; 2012-04-19T03:48:24.
#EndFunction
#DefineFunction parseit(json)
text = json
text = StrReplace(text,",","=")
text = StrReplace(text,"|","")
text = StrReplace(text,@LF,@CRLF)
Return text
#EndFunction
Return
;====================================================================================


Title: Re: Morse Code url
Post by: JTaylor on February 09, 2021, 07:33:30 AM
This probably isn't what you mean but if using the individual sound files works you wouldn't need to translate.....

https://commons.wikimedia.org/wiki/Category:Audio_files_of_Morse_code


Jim
Title: Re: Morse Code url
Post by: stanl on February 09, 2021, 08:17:02 AM
Quote from: JTaylor on February 09, 2021, 07:33:30 AM
This probably isn't what you mean


it isn't.




$Text = 'Winbatch 2021'

# URL-encode text
Add-Type -AssemblyName System.Web
$encoded = [System.Web.HttpUtility]::UrlEncode($Text)

# compose web service URL
$Url = "https://api.funtranslations.com/translate/morse.json?text=$encoded"

# call web service
$morse = (Invoke-RestMethod -UseBasicParsing -Uri $url).contents.translated

Foreach ($char in $morse.ToCharArray())
{
    switch ($char)
    {
        '.'      { [Console]::Beep(800, 300) }
        '-'      { [Console]::Beep(800, 900) }
        ' '      { Start-Sleep -Milliseconds 500 }
        default  { Write-Warning "Unknown char: $_"
                   [Console]::Beep(2000, 500 ) }

    }
    Write-Host $char -NoNewline
    Start-Sleep -Milliseconds 200
}
Write-Host "OK"