Run a Lua Script from LiveCode

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
McJazz
Posts: 111
Joined: Sun Apr 04, 2010 5:59 am

Run a Lua Script from LiveCode

Post by McJazz » Mon May 16, 2011 12:29 am

Does anyone know how to run a Lua Script from LiveCode?

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Run a Lua Script from LiveCode

Post by SparkOut » Mon May 16, 2011 10:25 am

I'd imagine only by using a shell command. Is there a console application you would use to run a Lua script? (For example, with a vbscript you could open a command prompt and type

Code: Select all

>cscript.exe "myVBScript.vbs"
and have it execute in that context. (I know you could also leave out the "cscript.exe" and have it execute, but that would open in the wscript context which means you wouldn't be able to pick up results in the same manner, if that's important. Anyway the point is to try an get a way to work with Lua, so this is just an indication of how it might be possible to adapt it to get it to interact.)
Say I have an external file somewhere containing vbscript:

Code: Select all

result = InputBox("What is your favourite colour?")
WScript.echo result
In Livecode I could

Code: Select all

put "<the path to my vbscript file>" into tVBScriptFile

set the hideConsoleWindows to true
--the above line stops the black command prompt window from appearing on screen, however briefly

get shell("cscript.exe //nologo" && quote & tVBScriptFile & quote)
-- specifying cscript tells Windows to use the console interpreter for the vbscript execution, 
-- rather than the Windows GUI interpreter
-- using the "nologo" switch means that the console result is not cluttered with the standard
-- Microsoft copyright text, which means it is easier to just pick up the returned value which is
-- placed in "it". (Note this is rather different from using a "do as VBScript" operation from
-- within Livecode directly, where a variable "result" in the VBS code is returned to Livecode
-- in "the result. But let's not muddy the waters with that now, the purpose here is to 
-- see what can be adapted to get Lua scripts to work, which means a shell flavour is needed.)

put it into theReturnedValue
-- the Wscript.echo as the last line in the vbscript file sent that value to the console. Which
-- we retrieved with our "get" command, and can now use the value in "it"

answer "The VBScript routine told me that your favourite colour is" && theReturnedValue
-- so we got a value back from an external script.
So the same technique should be able to be applied to trigger, and hopefully retrieve results
from a Lua script.
You may also find there is some mileage in a "start" command of the sort:

Code: Select all

get shell ("start" && quote & quote && quote & thePathToTheLuaCommand & quote && quote & "thePathToTheLuaScript" & quote)
depending on how you might call a Lua script from a command prompt. You might need to adjust the quoting, say wrapping the whole command and script paths in one set of quotes, for instance. Note that there is a "dummy" set of double quotes between the "start" and the (quoted) path - this is to act as an empty "placeholder" for a "title" of the console window (which you would have hidden anyway).

I hope this is at least helpful in some ways to give you a direction to go about adapting things to work with your Lua scripts.

QuangNgo
Posts: 60
Joined: Thu Mar 31, 2011 8:31 am
Location: HCMC VietNam

Re: Run a Lua Script from LiveCode

Post by QuangNgo » Fri May 20, 2011 11:45 am

Hi guys

I had a VBS file as I do as you said above ,It work well !
Do you know to how to put parameter from textbox of Livecode to InputBox of VBS?

Thanks you guys a lot
Quang

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Run a Lua Script from LiveCode

Post by SparkOut » Sat May 21, 2011 1:04 am

You should be able to pass the parameters to the VBScript when you invoke it in the "get shell.." command from Livecode.
Within the VBScript file you can find the paramenters passed to it as arguments:

Code: Select all

get shell("cscript.exe //nologo" && quote & tVBScriptFile & quote && quote & tParameter1 & quote && quote & tParameter2 & quote)
(You may need to watch the number of quotes and spaces, and possibly even wrap the whole thing in an extra set of double quotes, which is what I had to do in some circumstances back on my Windows XP machine).

Within your VBScript you should be able to retrieve those results by:

Code: Select all

If WScript.Arguments.Count = 2 Then
    'the first parameter from Livecode can be the InputBox title
    tTitleFromLivecode = WScript.Arguments.Item(0)
    'the second parameter from Livecode can be the InputBox
    tDefaultContentFromLivecode = WScript.Arguments.Item(1)
Else
    Wscript.Echo "Error: I was expecting two parameters to be passed from Livecode"
    Wscript.Quit
    'or whatever error handling
End If
tInputResult = InputBox("This input box should have a title of " & tTitleFromLivecode & _ 
" with a default content of " & tDefaultContentFromLivecode,tTitleFromLivecode,tDefaultContentFromLivecode)
Wscript.Echo tInputResult

QuangNgo
Posts: 60
Joined: Thu Mar 31, 2011 8:31 am
Location: HCMC VietNam

Re: Run a Lua Script from LiveCode

Post by QuangNgo » Sat May 21, 2011 5:01 am

Thanks SparkOut
No words to say Thank you a lot :D
Quang

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Run a Lua Script from LiveCode

Post by SparkOut » Sat May 21, 2011 11:47 am

Glad to help.
I also note that we are discussing VBScript here again, where the attempt was to find something that could be adapted to use a Lua script. Livecode can actually "do" a vbscript directly, without having to have an external file. The technique is subtly different in that the result is retrieved in a different way, and you can't (as far as I know) pass parameters to the script with this method. However, you can (as I believe you have already seen in discussion with Klaus on your other thread about AD) put a placeholder in the vbscript and replace the placeholder with contents of a variable before execution.
A little sampler stack shows this in action. Hopefully you can also use the above "shell" technique to get Lua scripts to run, and return a value somehow to Livecode.
Attachments
VBScript tester.zip
(1.81 KiB) Downloaded 298 times

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”