Saving my sanity with Zenity : shell script interaction via the GUI

Saving my sanity with Zenity : shell script interaction via the GUI


Whilst an increasing number of recent converts are avoiding it (and I don't blame them really), the shell is still a key tool for the majority of GNU/Linux users. Shell scripts are knocked-up, shared and deployed in all sorts of circumstances -- some simply time-saving, others life-saving. But even if the shell script has been written by somebody else, running it can be a cumbersome and frightening exercise for users of lesser experience or confidence. How do we bring the flexibility of the shell script to the GUI-only user? Recently faced with just such a quandary, I discovered Zenity: a tool which just might have saved my sanity.

What's a shell and why does it need a script?

Before I get ahead of myself, the shell is an interface with which you can enter commands to your computer. There are no widgets, drop-downs or buttons - just a place to type commands. On Windows boxes the shell is often called the command line (or CLI). On most GNU/Linux systems the shell will be what you see and use if you open a terminal. Shell scripts are text files containing shell commands and their parameters to be run in a certain order. Although there are some slight differences, in general the commands you type in a terminal can be put into a script and you get your system to run them by calling said script. Shell scripts can also be interactive: asking for input and decisions from the user before proceeding on a particular course.

Running scripts from the GUI

You can run shell scripts from the GUI of course: place a short-cut to one on your Desktop and it will execute itself in the background or in a terminal window (at your preference) when you run it. The problem is that for a GUI user is that the interaction is so different to the rest of your computing experience. In my experience, users accustomed to a dialog with Yes/No buttons can find themselves stumped by a black window with a bunch of white text, the last line of which says something like Interface not found: proceed (Y/N)?.

Examples of Zenity dialogsExamples of Zenity dialogs

Zenity is not a unique tool (it's similar to dialog and Xdialog etc.) and not the first of its kind either, but it is very usable. The FSF directory page says that "Zenity lets you display Gtk+ dialog boxes from the command line and through shell scripts" and I can find no better way of summarising it. Put another way though, Zenity allows a shell script to interact with a GUI user in a mutually beneficial way. Sometimes your shell script needs to ask the user a question: Zenity gives them a familiar dialog window and accepts the answer accordingly. The end user need not even be aware that they are running a shell script.

Hello world!

As mentioned, you can put shell commands into scripts to be executed. The same applies the other way around. You can take a script command and run it directly in the shell if you want. In the case of Zenity this is less useful unless you are doing for testing purposes though. To give a simple example in the standard fashion this is how you would get Zenity to display an information dialog. Assuming you have Zenity installed (it's a package on most GNU/Linux systems so just search for it in your usual package/software manager), type this directly into a GUI terminal emulator (xterm etc.) and see what you get:

zenity --info --text "Hello World\!"

If you put that into a shell script with a shortcut to it on the desktop, you'll get the same result without the need for a terminal window. That's all well and good, but not very interactive. Zenity, of course, can do more than information dialogs. Give it the question parameter and you get something more useful. zenity --question --text "Do you want to proceed?" gives you a question dialog which returns 0 or 1 when the user clicks the Ok or Cancel buttons accordingly. Remember that Zenity is designed for simple interaction with shell scripts, so you are limited to the OK/Cancel button labels. In practice this means wording your question nicely or explaining in the dialog text the effect each button will have. If you need something more, then you might need to look at reworking your script into a GUI-capable language (say Python for example). Other dialog types for Zenity include calendar, file open/save, progress indicator and warnings. In other words: if there was some interaction your shell script wanted with the end user you can probably replicate it in a Zenity dialog.

How Zenity saved my sanity

A colleague of mine has recently acquired an Asus EeePC 4g and very pleased she is with it too. I understand it receives significant green eyed glances from sales reps with their desktop replacements on train journeys. Recently she acquired a mobile broadband (3G) dongle to give her web connection when away from the office or a wireless hotspot. The USB device came with a (albeit binary -- I'm working on a free solution) GNU/Linux driver on the accompanying CD and also one specifically labelled for the EeePC -- result. The only problem was that the driver needed to be [un]loaded manually by a shell script each time you want to use it. Whist a udev solution is possible, we didn't want the modem to connect up each time the dongle was plugged it. In addition the script allows for re-connecting and dropping the connection. The shell script provided does quite a bit and present a simple and fairly straightforward set of parameters: up, down and restart. It did need superuser privileges but sudo took care of that.

The problem here is that my colleague wasn't really the sort who would relish pressing Ctrl+Alt+T to get a terminal up and them entering sudo /path/to/connectionscript.sh up just to connect to the Internet (and then remember to replace up with restart if the connection had dropped). Another problem was that the provided script had no logging facilities and in order to later diagnose any issues I wanted to log some details of the connections when made. Finally, when you called the script to connect up, there was some delay in feedback until the connection was made which I felt could have made inexperienced users nervous. Keen to improve on the user experience provided by the script I wrote a wrapper script which detected whether the connection was up or down (and whether it had been dropped without the user's knowledge). Based on that it then asked the user to confirm what it was about to do and called the original script, piping the output to a log-file.

It worked a treat: the user now needed to remember a single command to connect, re- or dis-connect the connection. It was still a shell command and still required the terminal invocation. I could put a desktop icon on but the user still encountered the "weird black screen" (as I have heard it called). Zenity saved my brain here. I discovered it via googling and immediately I was able to replace all my user interaction with zenity calls. Now the script is run from a desktop icon, it detects if the connection is live, closed or dropped and asks the user to confirm what she wants it to do. It then provides a progress bar dialog while it attempts to do what she asked. Upon completion it gives her either a successful info dialog, a warning or an error. Everything is logged as I needed (managed by logrotate of course ) and the user is more than happy.

That's not all folks!

In the second part of this piece, I'll delve a little deeper into how to achieve what I did with Zenity. So expect more on progress, error, warning and question dialogs. If this has whetted your appetite, search on-line for zenity examples as I did. There is some good stuff out there.

Category: 

Author information

Ryan Cartwright's picture

Biography

Ryan Cartwright heads up Equitas IT Solutions who offer fair, quality and free software based solutions to the voluntary and community (non-profit) and SME sectors in the UK. He is a long-term free software user, developer and advocate. You can find him on Twitter and Identi.ca.

Most forwarded

Interview with Dave Mohyla, of DTIDATA

Dave Mohyla is the president and founder of dtidata.com, a hard drive recovery facility based in Tampa, Florida.

TM: Where are you based? What does your company do?
DTI Data recovery is based in South Pasadena, Florida which is a suburb of Tampa. We have been here for over 10 years. We operate a bio-metrically secured class 100 clean room where we perform hard drive recovery on all types of hard disks, from laptop hard drives to multi drive RAID systems.

Anybody up to writing good directory software?

Since the very beginning, directories (of any kind) have had a very central role in the internet. (I have recently grown fond of Free Web Directory. Even Slashdot can be considered a directory: a collection of great news and invaluable user-generated comments. As far as software is concerned, doing a quick search on Google about software directories will return the free (as in freedom) software directories like Savannah, SourceForge, Freshmeat and so on, followed by shareware and freeware sites such as FileBuzz, PCWin Download Center and All Freeware (great if you're looking for shareware and freeware, but definitely less comprehensive than their free-as-in-freedom counterparts).

Interview with Mark Shuttleworth

Mark Shuttleworth is the founder of Thawte, the first Certification Authority to sell public SSL certificates. After selling Thawte to Verisign, Mark moved on to training as an astronaut in Russia and visiting space. Once he got back he founded Ubuntu, the leading GNU/Linux distribution. He agreed on releasing a quick interview to Free Software Magazine.

Is better education the key to finding better software?

I read David Jonathon's article Anybody Up To Writing Good Directory Software? the other day, which got me thinking about software directories in general. As David mentioned, many of the software directories one finds when doing a quick google search are free as in beer, not as in freedom. But what interests me is the software directories that already exist, providing a combination of both free as in beer software, and open source software. Sites such as Freeware Downloads and Shareware Download don't advertise themselves as providing free as in liberty software, but each of them have a good selection of open source software available... if you know where to look.

Most emailed

Free Open Document label templates

If you’ve ever spent hours at work doing mailings, cursed your printer for printing outside the lines on your labels, or moaned “There has got to be a better way to do this,” here’s the solution you’ve been looking for. Working smarter, not harder! Worldlabel.com, a manufacture of labels offers Open Office / Libre Office labels templates for downloading in ODF format which will save you time, effort, and (if you want) make really cool-looking labels

Creating a user-centric site in Drupal

A little while ago, while talking in the #drupal mailing list, I showed my latest creation to one of the core developers there. His reaction was "Wow, I am always surprised what people use Drupal for". His surprise is somehow justified: I did create a site for a bunch of entertainers in Perth, a company set to use Drupal to take over the world with Entertainers.Biz.

Update: since writing this article, I have updated the system so that the whole booking process happens online. I will update the article accordingly!

So, why, why do people and companies develop free software?

More and more people are discovering free software. Many people only do so after weeks, or even months, of using it. I wonder, for example, how many Firefox users actually know how free Firefox really is—many of them realise that you can get it for free, but find it hard to believe that anybody can modify it and even redistribute it legally.

When the discovery is made, the first instinct is to ask: why do they do it? Programming is hard work. Even though most (if not all) programmers are driven by their higher-than-normal IQs and their amazing passion for solving problems, it’s still hard to understand why so many of them would donate so much of their time to creating something that they can’t really show off to anybody but their colleagues or geek friends.

Sure, anybody can buy laptops, and just program. No need to get a full-on lab or spend thousands of dollars in equipment. But... is that the full story?

Fun articles

Santa Claus - the most successful open source project

It dawned on me the other day, as I was shopping for the dozens of gifts it seems I have to buy every December, that Santa Claus is the most successful open source project in history. (Bridget @ Illiterarty would agree with that). Santa Claus is essentially a marketing development that is embodied by everyone who stuffs a sock, gives a gift, hosts a dinner or wishes Merry Christmas over the holiday season.

Most emailed

Editorial

When I first started thinking about Free Software Magazine, I was feeling enthusiastic about the dream. I had Dave, Gianluca, and Alan willing to help me, I had established members of the free software community willing to help me out, I had writers volunteering their time and energy for free, and I had a generous offer from OpenHosting for servers, all before I'd proved myself. There was a sense of excitement in the air, and I thought maybe, just maybe, I could make this work.

Free Software Magazine uses Apollo project management software and CRM for its everyday activities!