Introduction to Zenity (Part 1)

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
322
Reaction score
329
Credits
10,684
Zenity is a command-line utility for Linux that allows developers and users to create graphical user interfaces (GUIs) for shell scripts and other command-line applications. It provides a simple and easy way to display dialog boxes, information messages, input forms, and other types of windows within the desktop environment.

I cover a little of the use of Zenity in the article on Nautilus Scripts. But, I really only scratch the surface on the subject.

More can be done with using scripts anywhere with Zenity to allow the use of some Graphical User Interface (GUI) interaction from within any scripting language. The examples I use are all Bash based.

The rest of the parameters, not covered in this article, are in the next one (here) when it is available.

Syntax

To use Zenity, you start with the command 'zenity', followed by the parameters. The parameters are as follows:

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-color-selection Show color selection options
--help-password Show password dialog options
--help-forms Show forms dialog 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
--color-selection Display color selection dialog
--password Display password dialog
--forms Display forms dialog
--display=DISPLAY X display to use


Let's look at the Application Options to see how we can use Zenity with some examples.

Calendar

The Calendar option allows you to display a calendar and get a date input from the user. The Calendar syntax is as follows:

--calendar Display calendar dialog
--text=TEXT Set the dialog text
--day=DAY Set the calendar day
--month=MONTH Set the calendar month
--year=YEAR Set the calendar year
--date-format=PATTERN Set the format for the returned date


So, let's look at an example to have the user select a date. If we specify no day, month or year, the calendar will use the current date for the calendar when it appears:

Date1=$(zenity --calendar --text "Choose Date" --title "Date")

You can see in Figure 1 the placement of the title and text specified by the command. We store the value of the date chosen by the user in the variable 'Date1'. If the user selects cancel, then the value of 'Date1' is empty.

Figure 1.JPG

FIGURE 1

If you wanted to specify a date to start in the calendar, such as January 1, 2001, then the command is:

zenity --calendar --text "Choose Date" --title "Date" --month 1 --day 1 --year 2001

The 'PATTERN' for the date format is the same as you use for the 'date' command.

NOTE: You may notice that the window is small but enough to hold the Calendar. All the following GUI windows are all the same, just big enough for the contents. You can use the parameters '--height value' and '--width value' to enlarge the windows. The values you specify are in pixels.

Entry

The Entry option for Zenity will allow you to get text input from the user. You can then use this input within your script. The parameters for the Entry option are:

--entry Display text entry dialog
--text=TEXT Set the dialog text
--entry-text=TEXT Set the entry text
--hide-text Hide the entry text


We use the '--entry' parameter to specify that we need an entry box. We will place the title of the windows in the parameter '--text'. We can specify the default value to place in the text box by placing it in quotes, or use an existing variable with the parameter '--entry-text'.

Text1=$(zenity --entry --entry-text=$USER --title "Enter your Username")

I show the example in Figure 2. The sample text placed in the text box is the current user. If the user presses enter, then it uses the default value and places it in the variable 'Text1'.

Figure 2.JPG

FIGURE 2

If you want the text hidden by dots, then use the parameter '--hide-text'. Just be aware it doesn’t encrypt the variable contents and is visible within the variable if you echo it to the screen. It also stores the value in memory as plain text.

Error

You can generate error interfaces to alert the user to an error message. The syntax is:

--error Display error dialog
--text=TEXT Set the dialog text
--icon-name=ICON-NAME Set the dialog icon
--no-wrap Do not enable text wrapping
--no-markup Do not enable Pango markup
--ellipsize Enable ellipsizing in the dialog text. This fixes the high window sizewith long texts


The window generated is an error window with a default icon of 'dialog-error' as shown in Figure 3 made by the command:

zenity --error --text="Error Text" --title="Error Title"

Figure 3.JPG

FIGURE 3

If you use the parameter '--no-wrap', then it displays the text on one line and the box widened to support the length.

Notice that I used the parameter '--title' to override the default of title of 'Error'.

Let's look at the parameter '--icon-name'. You can specify an icon that is on the system. This does not mean an icon file, but an icon installed in the system defaults. To see the list and names of the system icons, install 'gtk-3-examples'. Once installed, run the program 'gtk3-icon-browser' to see the icons with their names. Just specify the name with the parameter and it displays the icon in the Zenity window.

The Pango Markup is like HTML. An example is:

zenity --info --text '<span foreground="blue" font="50">BIG</span>\n\n\n\n<i>(big and blue)</i>' --title "Pango Markup"

I show the output in Figure 4 and you can manipulate the text and font as you wish.

Figure 4.JPG

FIGURE 4

Using the '--no-wrap' parameter will allow you to specify to wrap text that is so long to prevent it wrapping to a second line. Using the '--ellipsize' to place the three dots to break a line at the end where there is more to show.

Info

Other than an error interface, you can also generate an information message to the user. The parameters are:

--info Display info dialog --text=TEXT Set the dialog text --icon-name=ICON-NAME Set the dialog icon --no-wrap Do not enable text wrapping --no-markup Do not enable Pango markup --ellipsize Enable ellipsizing in the dialog text. This fixes the high window size with long texts

The parameters are the same as '--error', but the icon is different, as shown in Figure 5.

Figure 5.JPG

FIGURE 5

File Selection


If you need the user to select folders or files, then this is the parameter to allow for this. The parameters are as follows:

--file-selection Display file selection dialog
--filename=FILENAME Set the filename
--multiple Allow multiple files to be selected
--directory Activate directory-only selection
--save Activate save mode
--separator=SEPARATOR Set output separator character
--confirm-overwrite Confirm file selection if it already exists
--file-filter=NAME | PATTERN1 PATTERN2… Set a filename filter


Let's say I want a user to select JPG files. I can set the pattern to '.jpg' and '.JPG'. I must set both lowercase and uppercase extensions to get both since the pattern is case sensitive.

FILE=$(zenity --file-selection --title="Select a File" --multiple --file-filter "*.jpg")

case $? in
0)
echo "Seleced file(s):"
echo $FILE | tr "\|" "\n";;
1)
echo "No file selected.";;
-1)
echo "An unexpected error has occurred.";;
esac


Since I do not specify '--directory', I am only allowed to select files. I do not set the separator, so the default is a pipe, '|' I can split multiple files as shown in the line 'echo $FILE | tr "\|" "\n"'. I am replacing the pipe with a newline so the files are all printed on a different line, if the user makes multiple selections.

The 'case' is checking the exit value of the zenity command, where '0' means it was successful.

You can use the '--save' to save a file and with '--confirm-overwrite' to have the user prompted for a Yes/No to overwrite the file if it exists. An example is:

File=$(zenity --file-selection --save --confirm-overwrite)

If a file is to be written/overwritten, then the variable 'File' will contain the path and filename. The rest of the script can manage writing the file.

List

The 'list' parameter will let you generate a list and even allow you to make a selection. The parameters are as follows:

--list Display list dialog
--text=TEXT Set the dialog text
--column=COLUMN Set the column header
--checklist Use check boxes for the first column
--radiolist Use radio buttons for the first column
--imagelist Use an image for the first column
--separator=SEPARATOR Set output separator character
--multiple Allow multiple rows to be selected
--editable Allow changes to text
--print-column=NUMBERPrint a specific column (Default is 1. 'ALL' can be used to print all columns)
--hide-column=NUMBER Hide a specific column
--hide-header Hide the column headers
--mid-search Change list default search function searching for text in the middle, not on the beginning


You can perform a basic file selection with the script:

File=$(zenity --list --multiple --column="Files" $(ls))
echo "Files: $File"


Zenity will let the user select multiple files from a list generated from the current folder. It lists the files in the variable 'File' and it delimits each with a pipe.

You can specify multiple columns, shown in Figure 6, as shown by:

File=$(zenity --list --multiple --title "Choose file(s)" --column="Files" --column="Size" $(ls -l | awk '{print $9,$5}'))

Figure 6.JPG

FIGURE 6

The 'awk' command is letting me specify the output of the filename, $5 or column 5 from the command 'ls', and the size column, %9.

Most of the parameters are easy to understand. Be aware that a 'radiolist' lets you choose one item and a 'checklist' lets you choose multiple.

Notification

A Notification box is simply a message sent to the system to be displayed like a system notification, as shown in Figure 7.

Figure 7.JPG

FIGURE 7

The syntax is:

--notification Display notification
--text=TEXT Set the notification text
--listen Listen for commands on stdin
--hint=TEXT Set the notification hints


The '--listen' parameter will allow you to take information from 'stdin' to manage the notification box. The parameter seems to have issues and does not close Zenity when done.

If you use Zenity for a notification, it remains on the screen for 10 seconds or less if the user clicks on the notification area.

Conclusion

Zenity can be a powerful tool to add a GUI feel to a script.

The uses in this article are only a portion of the tools for Zenity. Be sure to read the next article.
 
Last edited by a moderator:

Members online


Latest posts

Top