Practical uses of the Zenity dialog box

I frequent the forums in my country a lot… and honestly, it is rare to find something interesting. However, in one of those forums, there is a user who has made two interesting posts about Linux, and this is the first one (which I want to share with you):

Practical uses of the Zenity dialog box

Zenity is a set of graphical dialog boxes that gtk libraries use, with this program we can enter data, select a list of tasks, show us the information resulting from a given process, allow us to make an interruption before or during a certain process, among others functions.

Zenity consists of around 13 graphical dialog boxes, let's see what these are and their possible combinations:

1- To show us a calendar and select a desired date (this date will be shown in numerical format once selected):

zenity --calendar

2- To enter text (very useful when requesting data or the name of a file)

zenity --entry

Combine them correctly so that it asks us for the data to enter

zenity --entry --text "Escriba el nombre del archivo"

3- To inform us that an error has occurred

zenity --error --text "Imposible continuar"

4- To select a file

zenity --file-selection $HOME

Adding this option allows us to select several files:
--multiple

With this you will select only the folders
--directory

With this instead of selecting we will be activating the save option
--save

With this we will prevent an existing file from being overwritten:
--confirm-overwrite

5- Show us certain information

zenity --info *text "Información a mostrar"

6- Show us a list of options and select one or a group of these:
zenity --list --column "nombre de columna" "opcion1" "opción2" "opción3" "opción4"

Now what happens if we want to have a list of actions for certain files, but we want the name of the action to be performed to be displayed. To achieve this we have to use these two options (–hide-column value and * print-column value) it would look like this:

zenity --hide-column 2 --print-column 2 --list --column "nombre de columna" --column "columna oculta" "nombre1" "comando1" "nombre2" "comando2"

If we want to select more than one action at a time, we must add these two options
Text that will separate the chosen sequence from the other (in this case we add this »» which means a space
--separator=" "

And the option that will allow us to select more than one action at a time
--multiple

7- Show us a notification in the menu bar

zenity *notification *text "Texto deseado"

8- Show us the progress of a given process:
zenity --progress --pulsate

9- With this it will show us a question and we can choose whether or not we want to continue with the process:

zenity --question --text "Desea Continuar"

10 With this we can receive the result of a sequence in the console such as the list of a file search, help queries, among others:

zenity --text-info zenity --help-all | zenity --text-info

11 This can inform us that the process has been interrupted.

zenity --warning --text "El proceso ha fallado" ls /media/carpeta || zenity --warning --text "No existe el directorio"

12 With this we can select a given number by means of a slider bar:

zenity --scale

By adding this option we can define the minimum value:
--value 60 --min-value 60
(the –value option should never be less than * min-value)

With this we select the maximum value
--max-value 100

13 With this it will show us a dialog box with which we can select a desired color and take the color from the other side with the so-called color selector dropper.
zenity --color-selection --show-palette

After seeing which are the dialog boxes available in this program, let's see some practical examples with these:

- Let's see how we can mix the text input dialog box
using a script:

#!/bin/bash

#Darle a una palabra una secuencia de comandos.

archivo="`zenity --entry --text "Escriba el nombre del archivo"`"

#comando para renombrar

mv "$@" "`dirname "$@"`"/"$archivo"

- Let's see how to combine the dialog box number 12 of numerical scales:
(This is a simple script to lower the quality of the jpg images and thus reduce the space it occupies on our disks)

#!/bin/bash

#Darle a una palabra una secuencia de comandos.

foto="`zenity --scale --value 80 --min-value 60 --max-value 100`"

#comando para comprimir la imagen

mogrify -compress jpeg -quality "$foto%" "$@"

- Let's see another example where we will use the color selector dialog box where we will add a frame of the color that we select to a photo:

#!/bin/bash

#Darle a una palabra una secuencia de comandos.

foto="`zenity --color-selection --show-palette`" marco="`zenity --entry --text "Seleccione el rango deseado 6x6"`"

#comando para agregarle el marco

mogrify -border $marco -bordercolor $foto "$@"

- Let's see an example with the error messages dialog box:

#!/bin/bash

rm "$@" || zenity --error --text "Imposible de eliminar esto es una carpeta"

As you can see, if someone tries to delete a folder, the process returns an error so the chain continues thanks to these || operators.

- Let's see now what we can do with the dialog box number 6, selecting the actions that we will perform on a certain file:

#!/bin/bash

actions="`zenity --multiple --separator="" --hide-column 2 --print-column 2 --list --column "nombre de columna" --column "columna oculta" "comprimir un 80%" " -compress jpeg -quality 80%" "Cambiar tamaño a 800x600" " -resize 800x600"`"

#Comando

mogrify$actions "$@"

Note that there is a space in the command box since it does not allow the text to start with a hyphen, therefore the delimiter in this case has to be null –separator = »».

- Let's see another example with the progress dialog box

#!/bin/bash

#Script para eliminar

zenity --question --text "Desea borrara las imágenes dentro de esta carpeta `basename "$@"`" && find "$@" -name *.jpg -delete | zenity --list --progress * pulsate

... Well this is it.

I hope I can bring you more posts that are interesting.

regards


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   rots87 said

    The only thing I used the zenity for was because winetricks asks for it to work well (at least in my arch) hehehe thanks for the tip

  2.   elav <° Linux said

    Zenity is used for some other tricks, such as the Xfce search engine 😀

  3.   Aaron Mendo said

    Thank you, thank you very much, good that you speak of Gtk + I have also used zenity I still do not understand it very well but later with the information you have given and with practice I may find a practical use for it.

    Greetings.

  4.   Neo61 said

    Hey from Camaguey (I have a French keyboard that won't let me find the two pinticos of the u… hehehe), could you explain well what this is for me? And if all this is done by console? It would be good if you posted some images of captures so that the examples are illustrated and that way it looks better what you mean

    1.    KZKG ^ Gaara said

      Actually, this article was not written by me, but by a user of one of our forums.
      Yes, all this is done by console, and… what is it for? Well, it's very simple: "knowledge."

    2.    manolox said

      An example of use that can be given to zenity for someone with the French keyboard.


      #! /bin/bash
      # Un cambiador de teclado

      ACTION=`zenity --width=0 --height=260 --list\
      --title "Selector de setxkbmap" --text "Elige tu teclado"\
      --column "Idioma"\
      "Español"\
      "Francés"\
      "Inglés"\
      "Gringo"\
      "Alemán"`

      if [ -n "${ACTION}" ]; then
      case $ACTION in
      Español)
      setxkbmap es && zenity --info --text "Teclado configurado correctamente a español" || zenity --info --text "Por alguna razón no fue posible cambiar el mapa de teclado."
      ;;
      Francés)
      setxkbmap fr && zenity --info --text "Dicho sea en francés: Teclado configurado correctamente a francés" || zenity --info --text "Por alguna razón no fue posible cambiar el mapa de teclado."
      ;;
      Inglés)
      setxkbmap gb && zenity --info --text "Dicho sea en inglés: Teclado configurado correctamente a inglés" || zenity --info --text "Por alguna razón no fue posible cambiar el mapa de teclado."
      ;;
      Gringo)
      setxkbmap us && zenity --info --text "Dicho sea en Gringo: Teclado configurado correctamente a Gringo" || zenity --info --text "Por alguna razón no fue posible cambiar el mapa de teclado."
      ;;
      Alemán)
      setxkbmap de && zenity --info --text "Dicho sea en alemán: Teclado configurado correctamente a alemán" || zenity --info --text "Por alguna razón no fue posible cambiar el mapa de teclado."
      ;;
      esac
      fi

      1.    manolox said

        Oh, what a shame. Copying and pasting directly does not work because when it becomes a comment it "disrespects" the line breaks after each backslash "\".

        For it to work, you have to change the backslashes followed by a line break for a simple space.
        I do not copy it again so as not to saturate the comments, but to be clear, from the third line to the eighth, both inclusive, it should be in a single line such that:

        ACTION = `zenity –width = 0 –height = 260 –list –title« Setxkbmap selector »–text« Choose your keyboard »–column« Language »« Spanish »« French »« English »« Gringo »« German »`

        1.    Manual of the Source said

          If you want to share code in the comments you can save it in the paste and paste the url in your comment. 🙂

          1.    manolox said

            Excellent tool. He didn't know there was such a thing in DesdeLinux.
            The next time a comment requires code I will use it.
            Thanks for the info. XD

  5.   Neo61 said

    AH ... and if you see that I am using Linux it is because I use both, but I am interested in getting well into Linux

  6.   Santiago said

    Very good article !! It is super useful zenity.

    I use it in a thunar script that resizes the selected images, and with a list I offer the sizes of the standard images.

    Regards!!

  7.   Hyuuga_Neji said

    I need you to tell me where to find the Zenity that because I already search and it is not in the repo I have…. How happy I am using a repo that I don't download myself… (Alert: Sarcastic mode detected in this comment)

  8.   Jose Sales said

    Let's see what do you think of this?
    #! / Bin / bash
    song = $ (zenity –width = 360 –height = 320 –title "Launcher" –file-selection –directory $ HOME)
    find "$ song" -name * .mp3 | sort –random-sort | head -n 100 | xargs -d '\ n' mpg123
    I still need to improve it a little