This topic was published by DevynCJohnson and viewed 1436 times since "". The last page revision was "".
- Topics - 444
Many programming languages have some way of coding a graphical interface. Some languages like Python have special bindings (PyGTK, PYQT, etc.) and others, like Visual Basic, have their own commands for designing GUIs. As a result, many shell-script writers probably wish there were something like that for shell scripts. Well, there is a special way to code a GUI for scripts in the script itself. A special program called "Zenity" can be used in scripts to make GTK+-based windows. Zenity is used like other commands in a script with parameters and such.
NOTE: Readers should have an understanding of shell scripting to fully grasp this article. Although knowing how to script is not entirely necessary.
Obviously, the script writer would make a script as usual. Now, the programmer can add Zenity commands or replace echo and read commands with windows that perform the same function.
The basic usage of Zenity is like this -
zenity --info --text="blah blah blah"
"zenity" is the command. "--info" declares the type of box/window. "--text=" creates the desired text to be displayed by this info window. The text parameter can display the output of commands and the value of variables. As many scriptwriters will see, Zenity uses the same syntax as shell scripts. Displaying the output of a command is as easy as this -
zenity --info --text=`whoami`
NOTE: Zenity also works in batch files (in MS-Windows). Here, Zenity would use the same syntax/rules of batch files. Also, keep in mind that Zenity is a third-party program and not a language, although I seem to make it sound like it is some programming language.
Zenity supports a few different kinds of dialogs as seen below. One useful parameter that can be used on all of them is the "--title=TEXT" parameter, where "TEXT" is the name desired for the window.
The message dialogs (--info, --question, --warning, --error) display some message via the "--text=" parameter. These dialogs create a window that displays a message. The differences between them lies in the icon shown and the type of buttons. For instance, "--question" makes a window with two buttons (yes and no) and a question-mark icon, and "--warning" has one button (okay) and an exclamation-point icon. These message dialogs can be used in place of the echo command.
The "--text-info" dialog is used to display a text file. For example, "zenity --text-info --filename="/home/user/file.txt" would open a text file to be viewed. "--font=FONT" is a parameter used to specify the font to be used. "--editable" makes the displayed text editable, and the text will be sent to stdout when the dialog is closed. Programmers can use "--text-info" to display license terms and have a check-box for accepting the terms (--checkbox=TEXT). HTML is support via the "--html" parameter. The html parameter is needed to use the url parameter which displays webpages in the window (--url=LINK).
Some of you may wonder how the script can execute differently based on whether the check-box associated with "--text-info" was checked or unchecked. The "$?" shell variable equals "1" when the box is unchecked and "0" when checked. Then, the programmer can have a decision construct that executes code based on the user's choice.
NOTE: The $? variable prints the error code of the previously executed command. No actual error is occurring though with Zenity unless you get a number other than zero or one.
Users can enter text in Zenity via the "--entry" dialog. The code (
zenity --entry --entry-text="Type" --text="Bang on the keyboard") makes a window with text giving the user some information (like directions or a question) and an input box. The typed text can be retrieved through the $? variable which will also include "/n0" if no errors occurred. The "sed" or "awk" command can be used to remove the zero and newline when only the entered text is needed. The "--hide-text" parameter makes the text entered in the input box look like dots. This is used when entering private data like a password.
NOTE: No encryption is used in Zenity. Please be aware of security risks.
The "--scale" dialog makes a window with a slider. To use a number slider, make code like this -
VALUE=`zenity --scale --text="Select a number" --value="20" --min-value="0" --max-value="200" –step="5"`
The selected number will be saved in the variable "VALUE". The backticks (also called "grave quotes") are a special form of syntax used in shell scripting to indicate that the code inside is to be executed and that it is not a string. The "--text=TEXT" displays the desired text in the window. The "--value=#" parameter specifies where to start the slider (default = 0). The "--min-value=#" and "--max-value=#" set the limits of the slider (default = 0 and 100, respectively). The "--step=#" parameter sets the incremental value (default = 1). Using the arrow keys to move the slider will yield the set stepping, but not so when using the mouse (a bug perhaps?). To hide the number shown above the slider, use "--hide-value".
When the script is processing data, a progress bar can be displayed (--progress). Zenity offers two kinds. One is the pulsating bar where a box moves back and forth (use the --pulsate parameter) and stops after an end-of-file (EOF) symbol is reached. The second progress bar fills according to percentage. To make such a window, make a command like this -
zenity --progress --text="Working hard or hardly working?" --percentage=0 --auto-close
The "--auto-close" parameter makes the window close when 100% is reached. The "--percentage=%" indicates where the bar starts. The bar is filled by piping an integer (number) to Zenity. This is done as seen below. After a command executes, the next echo command tells Zenity to fill the bar more. Once the last command executes, then the bar fills to zero and then closes (if --auto-close is present).
- Progress Bar -
(echo "40"; COMPLEX_COMMAND; echo "60"; COMMAND) | zenity --progress --text="Working hard or hardly working?" --percentage=0 –auto-close
- Pulsating Bar -
(COMMAND; COMMAND; COMMAND; COMMAND) | zenity --progress --text="Working hard or hardly working?" --percentage=0 –auto-close
To enter a username and password via Zenity, use a command like this -
PRIVATE=`zenity --password --username`
The username and password are saved to the variable PRIVATE. The username parameter is optional, so programmers can use this when only a password is needed. Again, be aware of security issues. If the user closes the window without entering data and clicking "Okay", then $? equals "1" like all other Zenity windows.
"--notification" displays a notification bubble or a window depending on the desktop interface. (
zenity --notification --text=TEXT)
To select file(s), use the "--file-selection" dialog. "--multiple" allows more than one file to be selected. "--directory" permits the selection of directories. When saving the list of selected files to a variable, the "--separator=STRING" parameter allows the programmer to set the string or character to be used as a division for items in a list. Be careful that the selected files do not contain the string that is to be used as a separator. Otherwise, the list will appear to have different/more files. "--filename=PATH" sets which file or directory is highlighted by default. To use this same dialog to save a file, use the "--save" parameter.
FILE=`zenity --file-selection --multiple --directory --separator="+++"`
To select a color, use the --color-selection dialog. "--color=#" sets the initially selected color, and "--show-palette" displays the palette set. The code may look like this -
COLOR=`zenity --color-selection --show-palette`.
A calendar dialog (--calendar) also exists. "zenity --calendar" will show a calendar with the current date highlighted. The "--day=#", "--month=#", and "--year=#" parameters set the time to be highlighted and shown when the window appears. The "--date-format=strftime" allows the programmer to set the time format in strftime format (where month is %m, year is %y, etc.). "--text=TEXT" is used to display a message on the window. The selected date can be saved to a variable.
"--list" makes a list dialog. The "--column=NAME" parameter can be used as much as needed to make a column of data. To make such a dialog, use a format like the one seen below. If a field contains spaces or other non-alphanum characters, place that field in quotes to indicate that the string is one set of data. If radio-buttons or check-boxes are included, make sure a column is made for them. The "--checklist" and "--radiolist" parameters place check-boxes and radio-buttons in the first column, respectively. "--editable" allows the data to be changed.
zenity --list \ --title="Choose your OS" \ --column="OS" --column="Interface" \ Ubuntu Unity \ "OS X" Marble \ FreeBSD "Command line" \ Fedora GNOME \ Minix Command_line \ Pidora XFCE \ Lubuntu LXDE \ "MS-Windows" Metro
The last dialog offered by Zenity is forms (--forms) which is used to enter many sets of data. For private data, the "--add-password=FIELD_NAME" parameter can be used to make an input box that does not display the contents. For text that can be seen, use the "--add-entry=FIELD_NAME" parameter. "--text=TEXT" displays a message on the window. "--add-calendar=FIELD_NAME" makes a calendar which is used to select a date. The calendar's output format is specified with "--forms-date-format="strftime". The "--separator=STRING" parameter is used to set how the fields are separated from each other in the output. After the Zenity command and its parameters, ">>" can be used to direct the data to a file. (
zenity --forms --PARAMETERS >> data.csv)
Now, that you know about Zenity, you can enhance your favorite scripts and make new ones that are more advanced than what you made before. Enjoy!