Menu

Small, dinamically generated GUI for Lua scripts

Here is a small application, which dynamically builds a form for the script parameter inputs.
The script should contain the input fields description.

The script location is the ./scripts folder (see FileListBox1.Directory property)

-- SAMPLE SCRIPT
-- vcltype; label; valueproperty; [defaultvalue]; [length]; [listitems]
-- space required for empty param value!

-- @ARGS
-- Edit;Text parameter;text; ;100
-- FileNameEdit;Output filename;filename;deafult.txt;260
-- DateEdit;From date;text; ;100
-- CheckBox;Click this;checked; ;true
-- ComboBox;Resolution;text; ;100;low|medium|high
-- @ENDARGS

io.stderr:write("\nScript arguments:\n")
for i,p in ipairs(arg) do
    io.stderr:write("arg["..i.."]\tlength="..string.len(p).."\t"..p.."\n")
end

Here is the VCLua application

-- SCRIPT WIZARD APP
package.path=package.path..";lua/?.lua"
package.cpath=package.cpath..";lib/?.dll;lib/?.so"

VCL = require "vcl"

local formArgs = {}
local CS = 24
local BTOP = 70

MainForm = VCL.Form(nil,"MainForm")
local Panel1 = VCL.Panel(MainForm,"Panel1")
local Panel3 = VCL.Panel(Panel1,"Panel3")
local Button1 = VCL.Button(Panel3,"Button1")
local Splitter1 = VCL.Splitter(MainForm,"Splitter1")
local Panel2 = VCL.Panel(MainForm,"Panel2")
local FileListBox1 = VCL.FileListBox(Panel2,"FileListBox1")
local Label1 = VCL.Label(Panel1,"Label1")

MainForm._ = { Left = 349,  Height = 396,  Top = 211,  Width = 595, ClientHeight = 396,  ClientWidth = 595,  Caption = "VCLua Script Wizard", }
Panel1._ = { Left = 0,  Height = 400,  Top = 0,  Width = 420,  Align = "alLeft",  ClientHeight = 400,  ClientWidth = 420,  TabOrder = 0, }
Panel3._ = { Left = 1,  Height = 43,  Top = 352,  Width = 385,  Align = "alBottom",  BevelInner = "bvRaised",  BevelOuter = "bvLowered",  ClientHeight = 43,  ClientWidth = 385,  TabOrder = 0, }
Button1._ = { Left = 159,  Height = 25,  Top = 8,  Width = 75,  Caption = "Run",  Enabled = false,  TabOrder = 0, }
Splitter1._ = { Left = 387,  Height = 396,  Top = 0,  Width = 5, }
Panel2._ = { Left = 392,  Height = 396,  Top = 0,  Width = 203,  Align = "alClient",  ClientHeight = 396,  ClientWidth = 203,  TabOrder = 2, }
FileListBox1._ = { Left = 1,  Height = 394,  Top = 1,  Width = 201,  Align = "alClient",  Directory = "./scripts",  ItemHeight = 15,  TabOrder = 0, }
Label1._ = { Caption = " ", Top = 3, Left = 10, font={style="fsbold,fsItalic", name="Courier New", size=9, color="clBlue"}}

function findargs(scriptName)
    local script = {}

    local f = io.open(scriptName, "r")
    while true do
        local line = f:read("*l")  
        if line == nil then break end
        table.insert(script,line)
    end
    f:close()

    local argh = {}
    local sa = nil
    for i,l in ipairs(script) do
        -- print(i,l)
        if string.find(l,"@ENDARGS") then sa = nil end
        if sa then
           table.insert(argh,string.sub(l,3):match("^%s*(.-)%s*$")) 
        end
        if string.find(l,"@ARGS") then sa = i end       
    end
    for i,l in ipairs(argh) do
        local n = 0
        local p = nil
        for word in string.gmatch(l, "[^;]+") do                
            n = n + 1   
            if n==1 then    
                local o = VCL[word](Panel1,{left=120,top=10+i*CS})              
                table.insert(formArgs, o)                           
            elseif n==2 then
                local o = VCL.Label(Panel1,{left=10, top=10+i*CS, caption=word})                
                formArgs[i]["labelcontrol"] = o
            elseif n==3 then
                p = word
                formArgs[i]["propertyvalue"] = word
            elseif n==4 and word~=" " then
                formArgs[i][p] = word
            elseif n==5 and word~=" " then
                formArgs[i].width=tonumber(word)
            elseif n==6 then                
                formArgs[i].items=string.gsub(word,"|","\n")            
            end
        end     
    end
end

function ScriptFileNameChange()
    if VCL.FileExists(FileListBox1.Filename) then
        Button1.Enabled = true
        Label1.Caption = FileListBox1.Filename
        local scriptName = FileListBox1.Filename
        if #formArgs>0 then
            for i,o in ipairs(formArgs) do          
                o.labelcontrol:Free()
                o:Free()
            end
            formArgs = {}
        end     
        findargs(scriptName)
    end
end

function ScriptRunButtonClick()
    local scriptArgs = {}
    for i,o in ipairs(formArgs) do
        local p = o.propertyvalue
        table.insert(scriptArgs,tostring(o[p]))
    end 
    local scriptName = Label1.Caption
    local result =  VCL.RunSeparate(table.maxn(scriptArgs),scriptName,unpack(scriptArgs))
    if #result>0 then
        VCL.MessageDlg("Script error:\n" .. table.concat(result,"\n"),"mtError",{"mbOk"})
    else
        VCL.MessageDlg("Script running finished.","mtInformation",{"mbOk"})
    end
end

FileListBox1.OnDblClick = ScriptFileNameChange
Button1.OnClick = ScriptRunButtonClick
MainForm:ShowModal()
Posted by Janos Bali 2014-06-06

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.