The 'zenity' utility

for issuing GUI prompts

in Linux scripts

EXAMPLES


Example 'zenity' GUI.

Home > RefInfo menu >

Computer topics menu (Linux section) >

This Zenity-Examples page

! Note !
Text, web-links and code-examples may be added or changed
--- if/when I re-visit this page.
 


SECTIONS BELOW:

INTROduction

Zenity Prompt Types
(Example code):

entry     fileselection     info

question     radiolist


More Info (web searches)     BottomOfPage

INTRODUCTION:

The zenity utility (man page link) is a command that can be used in Linux scripts to prompt the user for information in a pop-up GUI window.

The GUI that pops up can be of about 8 different kinds, including

  • entry field
  • radio-button list
  • question (yes/no answer)
  • info
  • file selection

This page is intended to show actual Linux script code examples of using the zenity command for these types of prompts.

These examples show how the user response can be stored in an 'environment variable' for use in later lines of the script.


I have used 'zenity' extensively in utility scripts that I created in a system named 'FE Nautilus Scripts' (where FE = Freedom Environment).

Those 'Nautilus' scripts are published at a Freedom Environment web site (www.freedomenv.com).

Those 'FE Nautilus' scripts provide more complete examples of zenity scripts that show how the 'enviroment variable' (that holds the user response) can be used in a wide variety of scripts. The 'FE Nautilus' scripts are grouped in categories such as

  • AUDIOtools
  • FILESlists
  • IMAGEtools
  • SPACElists
  • TERMtools
  • VIDEOtools

The code of the 'FE Nautilus' scripts can be seen via a menu on an FE Nautilus Scripts Description & Code Examples page at www.freedomenv.com.

You can browse the code of those scripts to see *COMPLETE* script examples that use the 'zenity' utility.


On an FE Nautilus Downloads web page (freedomenv.com link) there is a description of the install of the scripts into a user's home directory - for various Linux desktop 'flavors'.

  • MATE - $HOME/.config/caja/scripts

  • Gnome3 - $HOME/.local/share/nautilus/scripts (?)

  • Linux Mint Nemo - $HOME/.local/share/nemo/scripts

  • The old Gnome2 - $HOME/.gnome2/nautilus-scripts

If you use one of these desktop environments and put utility scripts in a directory like these, you may find it very useful to use 'zenity' in your utility scripts.

    A historical note:
    The Gnome2 'Nautilus' desktop and file-management environment was used in Ubuntu 9.10. When Gnome went to a different desktop system (Gnome3), the Gnome2 environment was so popular that some Argentine users used that software to develop the MATE (Wikipedia link) desktop environment, including a file-management system called 'Caja' (based on 'Nautilus').


To make it easy to find zenity code examples that may be useful to me, I decided to make this web page that puts the zenity examples in various categories on this page.

Besides making the examples easily available for my own use (anywhere there is an internet connection), this page is available to other Linux users who might find these examples helpful.

You can use the category links at the top of this page to go to the various categories of zenity prompts --- or use a text search in your web browser to find code examples of interest --- or simply scroll down this page to find zenity examples of interest.

zenity ENTRY examples:

The following 'zenity' example prompts the user for FRAMERATE in an 'entry' field.

    This code is from a script that takes an animated-GIF file as input and changes the frame-rate of the GIF. For example, if a GIF file plays back too rapidly, this script can make a new GIF file with a slower playback rate.

    The filename of the GIF file is in the (temporary, local) FILENAME environment variable.

Note that three parameters of the 'entry' option are supplied with text:

  • --title
  • --text
  • --entry-text

The 'title' text appears as the title at the top of the zenity popup window.

The 'text' text is multiple lines explaining the data that is being prompted for.

The 'entry-text' loads the entry field with an example value for the frame-rate --- which the user may change or take as a default by pressing the enter key after clicking on the window.

The following 'zenity' example prompts the user for two strings separated by a space.

    This code is from a script that takes a selection of files in a directory and changes 'string1' in the filenames to 'string2'.

The 'cut' command is used to extract 'string1' and 'string2' from the user response in (temporary, local) environment variable STRINGS1and2.

'string1' and 'string2' are stored in (temporary, local) environment variables STRING1 and STRING2.

Although 'zenity' does not allow for complex user interfaces with multiple entry fields, this separated-strings method can be used to ask for multiple data items.

zenity FILESELECTION example(s):

The following code-sample is from a script that uses a 'zenity --file-selection' prompt to prompt for a 'TO' directory for the copy of a bunch of selected files in the current directory.

The user response is stored in the (temporary, local) environment variable 'DIR_TO'.

The parameters '--directory' and '--filename' and '--confirm-overwrite' are specified.

See a 'zenity' man page for a description of these options.

zenity INFO example(s):

The following code-sample uses the 'zenity --info' command to show some information to the user.

In this case, the code comes from a script that plays the video files in a directory (one after another) using the 'mplayer' command.

This zenity-info window is used to show some keyboard controls of the Mplayer utility.

Note that the zenity command is terminated with the ampersand character (&) to cause the zenity command to be 'run in the background' so that the script can proceed to the 'mplayer' command.

This leaves the zenity-info window open while video(s) play.

The 'sleep 3' command allows the user a few seconds to see what the info window is about before popping up videos.

zenity QUESTION example(s):

The following code-sample comes from a script that plays a list of video files in the Totem (alias Gnome Video) media player program.

After the script plays one of the videos with the 'totem' command, the 'zenity --question' prompt is used to ask the user if they want to continue playing movie files that are in $FILENAMESFILE.

    FILENAMESFILE is a (temporary, local) environment variable that holds the name of the movie-filenames-file, which the user selected at the start of the script.

The zenity-question window presents 'No' and 'Yes' buttons.

If the user clicks 'No' (Cancel), the return code from zenity is 1.

If the user clicks 'Yes' (OK), the return code from zenity is 0.

See a 'zenity' man page for further description of the 'question' option.

zenity RADIOLIST example(s):

The following code-sample comes from a script that accepts multiple image filenames from a current directory and appends the selected image files together either horizontally or vertically.

After the user selects either 'Horiz' or 'Vert' from the radio-button list in the zenity GUI, the script will use the ImageMagick 'convert' command with either the '-append' (vertical) or '+append' (horizontal) option.

There are two '--column' parameters in the 'zenity' command that are used to provide column headings for the 2 columns --- of (1) radiobuttons and (2) their 'values'.

In this case, the first column heading is set to null (blank) and the 2nd heading is set to 'Type'.

The two radiobutton 'values' are set to 'Vert' and 'Horiz'.

The parameter FALSE turns OFF the 'Vert' radiobutton and the parameter TRUE turns ON the 'Horiz' radiobutton, as the default.

The '--title' and '--text' parameters of the 'zenity' command provide (1) the popup window title and (2) the user-guidance, like in most other invocations of the 'zenity' command.

The APPENDPARM is set according to the user choice and APPENDPARM is used to provide an append parameter to the 'convert' command that will follow this code.

    This example has two radiobuttons, but you could add more FALSE-and-Value pairs to have 3 or more radiobuttons --- with one TRUE pair.

More Info:
(esp. web searches)

For more info on using 'zenity' in scripts, you can try web searches on keywords such as the following.

Bottom of this page on
Zenity script code examples.

To return to a previously visited web page location, click on the Back button of your web browser, a sufficient number of times.

OR, use the History-list option of your web browser.

OR ...

< Go to Top of Page, above. >

Page history:

Page was created 2023 Dec 09.
(Started the 'Introduction' section and about 4 zenity-type sections.)

Page was changed 2023 Dec 10.
(Finished adding at least 1 example to 5 zenity-type sections.)