Home » Hello World in Lua Online Documentation

Hello World in Lua

What's Lua? "Lua is a powerful light-weight programming language designed for extending applications" : http://www.lua.org/

We're going to create our own script plugin using Lua- a really simple one that just inserts the words "Hello World" into the current page. I promise it'll be really easy.

First, create a new page in your VoodooPad document named "Hello World". When the page comes up, select everything and delete it. We're going to want a completely empty page to write this in.

Next, type (or cut and paste) the following line into your new page:

Now go to the Plugin->Lua->Run Page as Lua Plugin menu item. You should then see the text "Hello World" inserted into the page. Congratulations! That's all it takes to make a simple VoodooPad plugin. Now let's explain what's going on.

There is a variable in every lua script plugin we write named "windowController". This can be thought of as a reference to the window that the plugin is running in. We can then call various methods on the windowController, one such is textView(). This returns the editing portion of the window- the one where we actually do the typing in VoodooPad. We could have also written this plugin as:

But that's two lines and isn't as good a demo. But it is however a little bit clearer.

So windowController is a reference to window stuff, and textView is a reference to the editing text part of the window. Then when we call the method insertText(), passing in the string Hello World, we're saying insert the text "Hello World", in the text view of the current window.

That's all great- but how do we get our Hello World plugin up in the menu bar? That it turns out, is a piece of cake as well. Delete the text in the page that got inserted (the part that sayd "Hello ..." and make sure your page just looks like this:

Now, select the "Plugin->Lua->Save Page as Lua Plugin..." menu item, and VoodooPad will prompt you to save the plugin in the Script Plugins folder. Press the Save button.

Now if you go back to the "Plugin->Lua" menu, you'll see a new item in there named "Hello World". Select it, and tada! your new plugin was run. You can now use this plugin from any page within VoodooPad.

For more examples of script plugins, visit the Lua Plugin Snippets page.