How to gather Zenity output in Lua

Zenity is a library that allows you to spawn standard linux GUI controls in GNOME using GTK. It's not a Lua library, but rather a Linux package that you can interop with using your Lua logic for more easier and effective user input in GNOME desktop environments.

It is incredibly powerful for spawning small and simple GUI controls, such as file selectors, calendar date pickers, informational boxes, system notifications, etc.

To call this from Lua, you might think of using os.execute(), however that function isn't intended for you to capture the output of the operation past a simple true or false code signifying that it spawned and closed it correctly.

Instead, use io.popen(), which is another standard library function intended for capturing output. Essentially, it spawns a new process, runs the command in that process, and returns the result.

Here is an example that spawns a simple textbox control, gets the user input, then prints the result:

local proc = io.popen("zenity --entry --text='What is your favorite color?'")
local result = proc:read("l")
proc:close()

print(result)

In the :read() function, a clean way to gather the Zenity output is to use l for line when the output is only one line, and use a for all when your GUI's user input spans multiple lines.

More from gridlocdev's blog
All posts