Convert VBScript to Lua

Posted by Morat on Mon 21 Dec 2009 09:48 PM — 3 posts, 21,734 views.

#0
Is it possible to write the following scripts in Lua or should I write plugins in VBScript instead?

I rather not use the function os.execute since the DOS pop-up is annoying.

Should I try using LuaCOM? (faq 27)

Accessing COM objects from Lua
http://mushclient.com/forum/?id=6022

This VBScript alias copies my log to the temp directory then opens it in an editor.

<aliases>
  <alias
   match="log"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>Option Explicit
Sub Log
  Dim objShell, objFile, strLOG, strTMP, strEditor
  Set objShell = CreateObject("WScript.Shell")
  Set objFile = CreateObject("Scripting.FileSystemObject")
  strLOG = "C:\\Program Files\\MUSHclient\\logs\\DSL.LOG"
  strTMP = objShell.ExpandEnvironmentStrings("%TMP%") &amp; "\\DSL.LOG"
  strEditor = "C:\\Program Files\\Vim\\vim72\\gvim.exe"
  If objFile.FileExists(strLOG) Then
    objFile.CopyFile strLOG, strTMP
  End IF
  objShell.Run Chr(34) &amp; strEditor &amp; Chr(34) &amp; Chr(32) &amp; Chr(34) &amp; strTMP &amp; Chr(34)
  Set objFile = Nothing
  Set objShell = Nothing
End Sub
Log</send>
  </alias>
</aliases>


This VBScript alias searches an online equipment list. (i.e. submit form data, parse response, print)

<aliases>
  <alias
   match="search *"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>Option Explicit
Dim strHTML
Sub GetEquipmentList
  Dim objHTTP
  Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
  objHTTP.Open "POST", "http://www.dsl-mud.org/algoron/equipment/keyword_items.asp"
  objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  objHTTP.Send "keyword=" &amp; Replace("%1", " ", "+")
  strHTML = objHTTP.ResponseText
End Sub
Sub FormatEquipmentList
  Dim intX, intY, intZ, objRegExp
  intX = instr(strHTML, "#EDBD70")
  intY = instr(strHTML, vbCrLf &amp; "&lt;/div&gt;")
  intZ = intY - intX
  If intX = 0 Then
    strHTML = "no equipment found"
  Else
    strHTML = mid(strHTML, intX, intZ)
    Set objRegExp = New RegExp
    objRegExp.Global = True
    objRegExp.Pattern = "#EDBD70.."
    strHTML = objRegExp.Replace(strHTML, "")
    objRegExp.Pattern = "&gt;\\s+&lt;"
    strHTML = objRegExp.Replace(strHTML, "&gt;&lt;")
    objRegExp.Pattern = "&lt;tr&gt;"
    strHTML = objRegExp.Replace(strHTML, vbLf)
    objRegExp.Pattern = "&amp;nbsp;&lt;/td&gt;"
    strHTML = objRegExp.Replace(strHTML, " ")
    objRegExp.Pattern = "&lt;[^&gt;]*&gt;"
    strHTML = objRegExp.Replace(strHTML, "")
  End If
End Sub
GetEquipmentList
FormatEquipmentList
AnsiNote vbLf &amp; ANSI(32) &amp; strHTML</send>
  </alias>
</aliases>
Australia Forum Administrator #1
To load an editor, see:

Template:post=6581
Please see the forum thread: http://gammon.com.au/forum/?id=6581.


Something like this should work:


assert (package.loadlib ("windows_utils.dll", "luaopen_windows_utils")) ()

assert (windows_utils.shell_execute (
        strEditor,  -- program
        '"' .. strLOG .. '"'  -- argument
        )) 




As for querying a web page, try:

Template:post=8319
Please see the forum thread: http://gammon.com.au/forum/?id=8319.

#2
Thanks for reply. I got it all working.

Here is how to submit a web form with LuaSocket if someone else needs it.
local http = require "socket.http"
local requestbody = "keyword=" .. string.gsub("leather bracer", " ", "+")
local responsebody = {}
http.request {
  url = "http://www.dsl-mud.org/algoron/equipment/keyword_items.asp",
  method = "POST",
  headers = {
    ["content-type"] = "application/x-www-form-urlencoded",
    ["content-length"] = string.len(requestbody)
  },
  source = ltn12.source.string(requestbody),
  sink = ltn12.sink.table(responsebody)
}
local html = table.concat(responsebody)
print(html)

I got LuaCOM working to copy my log to the temp directory then open it in an editor.
assert(package.loadlib("luacom.dll", "luacom_open"))()
local sh = luacom.CreateObject("WScript.Shell")
sh:Run('cmd.exe /C copy /Y logs\\\\DSL.LOG %TEMP%\\\\DSL.LOG', 0, true)
sh:Run('"%ProgramFiles%\\\\Vim\\\\vim72\\\\gvim.exe" %TEMP%\\\\DSL.LOG', 1)

0 - Hides the window and activates another window.
1 - Activates and displays a window.

true - Wait for command to finish executing before continuing.