[ Impressum ]

LiveCode Server as a Command-Line Interpreter

www.Rozek.de > LiveCode Server > Installation as Command
While the "LiveCode Server" [1] is primarily intended as a CGI processor, you may also use it for command-line applications pretty easily.

This page describes the necessary steps.

Installation as Command-Line Processor

The basic "installation" of "LiveCode Server" has already been described in "LiveCode Lesson" "How do I install LiveCode Server?":
  1. download the "LiveCode Server" package which matches your operating system and
  2. unzip the package.
After unpacking, you will see that the "LiveCode Server" essentially consists of an executable program ("livecode-server") and two directories with associated libraries ("drivers" and "externals").

In principle, the "LiveCode Server" can now be used already (the "LiveCode Lesson" explains how to do this). However, it would be very inconvenient for the use as an interpreter of command line scripts if you would have to navigate to the directory containing the "LiveCode Server" and prepend "livecode-server" to the name of an actual script each time you would like to run such a scripted command...

Fortunately, Unix and Linux offer a comfortable way out of this situation using the so-called "shebang" line. But first, you should move "LiveCode Server" into a system directory:

sudo mv <LCServer-Folder>/* /usr/local/bin

moves the contents of the unpacked "LiveCode Server" directory to /usr/local/bin. Since the destination directory is part of the operating system, you will need to run the command as an administrator (hence the sudo at the beginning).

Additionally, you should create a (shell) script called lc-server in directory /usr/local/bin:

sudo cat > /usr/bin/env/lc-server << EOF
#!/usr/bin/env bash
livecode-server "$@" | tail -n +2
EOF

This script should be made executable:

sudo chmod +x /usr/bin/env/lc-server

Once this is done, you can write the following line at the beginning of each "LiveCode Server" script

#!/usr/bin/env lc-server

This is the aforementioned "shebang" line, and must always be inserted as the first line of a script file.

The example shown in the "LiveCode Lesson" should now look as follows:

#!/usr/bin/env lc-server
<?lc
put "Hello World!" && the date && the time
?>

In addition, the script (let us call it "myscript.lc" - as proposed in the "Live Code Lesson") has to be marked as executable:

chmod +x myscript.lc

Once this is done, you can invoke your script from the command line as follows:

./myscript.lc

(the characters "./" in front of the actual file name ensure that the command works regardless of your $PATH settings). As a result, you should get an output of the form

Hello World! 1/26/14 8:34 AM

The script can now be invoked without any explicit reference to the "LiveCode Server". The file name suffix ".lc" in our example is completely irrelevant - really important are only
  1. the "shebang" line at the beginning of script (#!/usr/bin/env lc-server)
  2. execution permissions of the script file (chmod +x <Skript-Datei>)
With this trick you can now create command-line scripts in LiveCode!

Have fun with "LiveCode Server"!

Bibliography

[1]
(RunRev Ltd.)
LiveCode | LiveCode Server Guide
The LiveCode Server is an interpreter for LiveCode scripts that is started from the command line (does not offer any graphical user interface) and is intended primarily as a CGI processor (this way, web pages can be processed with LiveCode - thus, you do not necessarily have to learn PHP any longer).