Standalone mode
Sometimes you wish to run code “outside” MC. (or: “independently” of MC)
In other words, you want to be able run your scripts from the command-line, just like you do with your Ruby/Perl/Python/whatever scripts of yours.
Guess what? —It’s possible.
To do this, use the ‘mcscript’ binary:
$ mcscript name_of_your_script.lua
Alternatively: begin your script with a “shebang” line, give it an executable permission, and you'll be able to execute it directly.
Let’s have an example.
Example
Put the following in a file named “listetc”:
#!/usr/bin/env mcscript -- List all the directories in /etc. for file in fs.glob("/etc/*/") do print(file) end
…turn on this script’s executable bit:
$ chmod +x listetc
…and run it:
$ ./listetc
In your script you have access to all of MC’s facilities: the virtual file system, the UI, etc.
Having a user interface
If you want to use the UI, go ahead and do it. Upon invoking most UI functions, the terminal will enter UI mode automatically:
#!/usr/bin/env mcscript local dlg = ui.Dialog() local moral_person = ui.Checkbox(T"I love MC") dlg:add(moral_person, ui.DefaultButtons()) if dlg:run() then if moral_person.checked then alert(T"The world needs people like you.") else alert(T"Scumbag!") end end
Explicitly entering UI mode
Some functions, like alert and devel.view, won’t enter UI mode for you: they're happy to work in non-UI mode. If you want them to use the UI, you need to start it yourself explicitly. You do this by calling ui.open:
#!/usr/bin/env mcscript ui.open() alert("Hi")
Command-line arguments
You can access the command-line arguments via the global variable argv.