zenity

From Glee
Jump to: navigation, search

Overview

Zenity is a neat little program which is very useful to interface shell scripting with a graphical environment. It uses GTK, so it has the GNOME look and feel. What is typically done with it is use it to prompt for user input and display feedback where one would normally use stdin and stdout.

Trivial Test

Just open up a terminal an type in :

zenity --question && echo yes || echo no

You'll see a nice dialog appear, asking you a question (with a configurable default text). The zenity command's exit status will depend on which one of the "Yes" or "No" buttons has been pressed.

Possibilities

There is little documentation, but luckily little documentation is needed. You basically just need to pass the main option to decide what type of dialog you want to create, then each type has its own options :

$ zenity --help
Usage:
  zenity [OPTION...]

Help Options:
  -h, --help                                     Show help options
  --help-all                                     Show all help options
  --help-general                                 Show general options
  --help-calendar                                Show calendar options
  --help-entry                                   Show text entry options
  --help-error                                   Show error options
  --help-info                                    Show info options
  --help-file-selection                          Show file selection options
  --help-list                                    Show list options
  --help-notification                            Show notification icon options
  --help-progress                                Show progress options
  --help-question                                Show question options
  --help-warning                                 Show warning options
  --help-scale                                   Show scale options
  --help-text-info                               Show text information options
  --help-misc                                    Show miscellaneous options
  --help-gtk                                     Show GTK+ Options

Application Options:
  --calendar                                     Display calendar dialog
  --entry                                        Display text entry dialog
  --error                                        Display error dialog
  --info                                         Display info dialog
  --file-selection                               Display file selection dialog
  --list                                         Display list dialog
  --notification                                 Display notification
  --progress                                     Display progress indication dialog
  --question                                     Display question dialog
  --warning                                      Display warning dialog
  --scale                                        Display scale dialog
  --text-info                                    Display text information dialog
  --display=DISPLAY                              X display to use

Each dialog type has its own small help :

$ zenity --help-question
Usage:
  zenity [OPTION...]

Question options
  --question                                     Display question dialog
  --text=TEXT                                    Set the dialog text
  --ok-label=TEXT                                Sets the label of the Ok button
  --cancel-label=TEXT                            Sets the label of the Cancel button
  --no-wrap                                      Do not enable text wrapping

Simple Example

Here is an original terminal based script :

#!/bin/sh

while test ! -d /media/USBDISK; do
  # Ctrl+C also works, but let's be more user friendly
  read -p "Please insert USB disk to perform the backup (enter 'c' to cancel) "
  [ "${REPLY}" == "c" ] && exit 1
done

rsync -avHP --delete ${HOME}/ /media/USBDISK/home

And now a zenityfied version :

#!/bin/sh

while test ! -d /media/USBDISK; do
  zenity --question --text="Please insert USB disk to perform the backup" --ok-label="Retry" --cancel-label="Cancel"
  [ $? != 0 ] && exit 1
done

rsync -avHP --delete ${HOME} /media/USBDISK/home | zenity --progress --text="Backing up ${HOME}..." --pulsate --auto-close --auto-kill

It's much more user friendly when meant to be executed from within a desktop environment.