• LINUX > zenity, pour afficher des boites de dialogue

      INSTALLATION

      sudo apt install zenity

      UTILISATION

      zenity --info --title="Mon titre" --text="Mon texte"

       

      14 types de fenêtre :

      --calendar Calendrier

      --entry Saisie de caractères

      --error Erreur à l’écran

      --file-selection Sélection d’un fichier

      --info Information

      --list Liste

      --notification Notification dans la zone prévue à cet effet

      --progress Progression

      --question Question ( OUI / NON )

      --text-info Du texte dans une fenêtre

      --warning Avertissement

      --scale Choisir une valeur numérique à l’aide d’un curseur

      --forms Formulaire

      --password Demande un mot de passe (peut être utilisé avec ‘sudo -S’)

      --color-selection Sélection de couleur

       

      Pour plus d’infos, consulter : manuel de Zenity.
      Script permettant la création de fenêtres Zenity de façon rapide et simple : zenitor 3.

      Coloration du texte

      zenity --info --text "<span color='#993333'>rouge</span><span color='#3333cc'>bleu</span>"

      Choix de la police pour le texte

      zenity --info --text "<span font-family='Arial'>Arial</span><span font-family='sans-serif'>sans-serif</span>
      <span font-family='Helvetica'>Helvetica</span><span font-family='impact'>impact</span>
      <span font-family='sans'>sans</span><span font-family='webdings'>webdings</span>"

      gras, italique…

      zenity --info --text "<b>gras</b><big>grand</big><i>italique</i><s>barré</s><sub>indice</sub>
      <sup>exposant</sup><small>petit</small><tt>espace entre les lettres</tt><u>souligné</u>"

      Options générales

      --title=TITRE Set the dialog title

      --window-icon=CHEMIN/VERS/ICONE Icone avec le chemin. Une des quatres stock icons can be used: ‘error’, ‘info’, ‘question’ or ‘warning’

      --width=X Largeur en X pixels

      --height=X Hauteur en X pixels

      --timeout Délai d’expiration de la boîte de dialogue en secondes

      Calendrier

      --text=STRING Set the dialog text

      --day=INT Renseigner le jour

      --month=INT Set the calendar month

      --year=INT Set the calendar year

      --date-format=PATTERN Format du retour de date. Exemple: %A %d/%m/%y

      Saisie de texte (entry)

      NOTE : Ne pas oublier les guillemets si il y a des espaces dans les réponses.

      zenity --entry --title="Jour" --text="jour de la semaine" --entry-text="Lundi" Mardi Mercredi "Autre jour..."

       

      if ret=`zenity --entry --text='Votre saisie:'` ; then
         bla=$ret
         if [ "$bla" = "" ] ; then
            echo "Saisie vide !"
            exit 1
         else
            echo "Bouton annuler !"
         exit
      fi
      echo $bla

       

      --text=STRING Set the dialog text

      --entry-text=STRING Entrée par défaut

      --hide-text Hide the entry text

      Erreur (error)

      --text=STRING Set the dialog text

      --no-wrap Do not enable text wrapping

      --no-markup Do not enable pango markup

      Sélecteur de fichier (file-selection)

      --filename=FILENAME Dossier to be selected by default

      --multiple Allow selection of multiple filenames in file selection dialog

      --directory Activate directory-only selection

      --save Active save mode

      --separator=SEPARATOR Specify separator character when returning multiple filenames

      --confirm-overwrite Confirm file selection if filename already exists

      --file-filter=NAME | PATTERN1 PATTERN2 Sets a filename filter

      Info

      --text=STRING Set the dialog text

      --no-wrap Do not enable text wrapping

      --no-markup Do not enable pango markup

      Liste

      --text=STRING Set the dialog text

      --column=STRING Set the column header

      --checklist Use check boxes for first column

      --radiolist Use radio buttons for first column

      --separator=STRING Set output separator character

      --multiple Allow multiple rows to be selected

      --editable Allow changes to text

      --print-column=NUMBER Specify what column to print to standard output. The default is to return the first column. ‘ALL’ may be used to print all columns.

      --hide-column=NUMBER Hide a specific column

      --hide-header Hides the column headers

      checkbox

      #!/bin/bash
      
      log1='thunderbird'
      log2='firefox'
      
      LOGICIELS=$(zenity --list --checklist --text="Logiciels à lancer" --column=" " --column="Logiciels" \
      FALSE "$log1" \
      FALSE "$log2" );
      
      if [ "${PIPESTATUS[0]}" != "0" ]; then
         exit
      fi
      
      # Lancer Thunderbird
      echo "$LOGICIELS" | grep "$log1" >/dev/null
      if [ "$?" = 0 ] ; then
         $log1&
         if [ "${PIPESTATUS[0]}" != "0" ]; then
            exit
         fi
      fi
      
      # Lancer Firefox
      echo "$LOGICIELS" | grep "$log2" >/dev/null
      if [ "$?" = 0 ] ; then
         $log2&
         if [ "${PIPESTATUS[0]}" != "0" ]; then
            exit
         fi
      fi

       

      Si tel dossier existe alors action 1, Sinon action 2

       

      # Lancer Thunderbird
      echo "$choice" | grep "$log1" >/dev/null
      if [ "$?" = 0 ] ; then
         if [ -d CheminDossier ]; then mv CheminDossier CheminDossier1 ; else AutreAction ; fi &
         if [ "${PIPESTATUS[0]}" != "0" ]; then
            exit
         fi
      fi

      A ce stade cela me fait toujours la premiere action, mais jamais la 2eme. Donc si vous avez quelques pistes, je suis preneur

       

      Pour info, tu peux simplifier le test externe :

      if grep -q "$log1" <<<"$choice"; then
         if [ -d CheminDossier ]; then mv CheminDossier CheminDossier1 ; else AutreAction ; fi &
         if [ "${PIPESTATUS[0]}" != "0" ]; then
            exit
         fi
      fi

       

      ${PIPESTATUS[0]} renvoie le code de retour de la première commande (0) d’un pipe.

       

      en utilisant des case ca marche::

      #!/bin/bash
      
      choice=$(zenity --list --checklist --text="Logiciels à lancer" --column=" " --column="Logiciels" \
      TRUE "gedit" \
      FALSE "gnome-terminal"\
      FALSE "nautilus");
      
      if [ "${PIPESTATUS[0]}" != "0" ]; then
         exit
      fi
      
      case "$choice" in
      log1=`echo $choice | cut -d'|' -f1 - `;
      log2=`echo $choice | cut -d'|' -f2 - `;
      log3=`echo $choice | cut -d'|' -f3 - `;
      notify-send "$log1  ;  $log2;  $log3  ";
      
      exit 0

      Notification

      --text=STRING Set the notification text

      --listen Listen for commands on stdin. Commands include ‘message’, ‘tooltip’, ‘icon’, and ‘visible’ separated by a colon. Exemple, ‘message: Hello world’, ‘visible: false’, or ‘icon: /path/to/icon’. The icon command also accepts the four stock icon: ‘error’, ‘info’, ‘question’, and ‘warning’

      Barre de progression

      --text=text Texte de la boîte de dialogue

      --percentage=Nbre Spécifier le pourcentage initial

      --pulsate Barre de progression qui pulse jusqu’à ce qu’un caractère EOF soit lu sur l’entrée standard.

      --auto-close Fermeture auto dès que 100% est atteint

      --auto-kill Tuer le processus parent si le bouton cancel est pressé

      --no-cancel Masquer le bouton cancel

       

      Si une ligne commence par ‘#’, le texte est mis à jour avec le texte de cette ligne.

      Si une ligne contient seulement un nombre, le pourcentage est mis à jour avec ce nombre.

      --progress s’utilise avec un pipe. Vous devez donc mettre tout le code affecté par la barre de progression entre parenthèses.

      #!/bin/sh
      (
         echo "10" ; sleep 1
         echo "# Mise à jour des journaux de mail" ; sleep 1
         echo "20" ; sleep 1
         echo "# Remise à zéro des paramètres" ; sleep 1
         echo "50" ; sleep 1
         echo "Cette ligne est ignorée" ; sleep 1
         echo "75" ; sleep 1
         echo "# Redémarrage du système" ; sleep 1
         echo "100" ; sleep 1
      ) |
      zenity --progress --title="Mise à jour" --text="Analyse des journaux de mail..." --percentage=0
      
      if [ "$?" = -1 ] ; then
        zenity --error --text="Mise à jour annulée."
      fi

      Question

      --text=STRING Set the dialog text

      --no-wrap Do not enable text wrapping

      --no-markup Do not enable pango markup

      --ok-label Set the text of the OK button

      --cancel-label Set the text of the cancel button

      Texte

      --filename=FILENAME Open file

      --editable Allow changes to text

      --checkbox=TEXT Enable a checkbox for use like a ‘I read and accept the terms.’

      --ok-label Texte du bouton OK

      --cancel-label Texte du bouton cancel

      Alerte (warning)

      --text=STRING Set the dialog text

      --no-wrap Do not enable text wrapping

      --no-markup Do not enable pango markup

      Valeur numérique avec curseur

      --text=STRING Set the dialog text

      --value=VALUE Set initial value

      --min-value=VALUE Set minimum value

      --max-value=VALUE Set maximum value

      --step=VALUE Set step size

      --print-partial Print partial values

      --hide-value Hide value

      Sélection de couleur

      --color=VALUE Set the initial color

      --show-palette Show the palette

      Password

      --username Display the username field

      Formulaire

      http://img11.hostingpics.net/pics/246564zenityforms.png

      --text=Texte Spécifier le texte affiché dans la boîte de dialogue de barre de progression.

      --separator=SÉPARATEUR Définit le caractère séparateur de sortie.

      --add-entry=Nom du champ Ajoute une nouvelle zone de saisie dans la boîte de dialogue de formulaire.

      --add-password="Bla" Ajoute une nouvelle zone de saisie de mot de passe dans la boîte de dialogue de formulaire.

      --add-calendar="Nom" Ajoute un nouveau calendrier dans la boîte de dialogue de formulaire.

      --forms-date-format=MODÈLE Définit le format de la date retournée. Exemple: %A %d/%m/%y

       

      #!/bin/bash
      
      # On crée le formulaire en stockant les valeurs de sortie dans $cfgpass :
      cfgpass=`zenity --forms --text="Définir un nouveau mot de passe" \
          --add-entry="Nom de l'utilisateur" \
          --add-password="Ancien mdp" \
          --add-password="Nouveau mdp" \
          --add-password="Confirmer le nouveau mdp" \
          --separator="|"`
      
      # Si on clique sur le bouton Annuler
      if [ "$?" -eq 1 ]; then
          exit 0
      fi
      # Pour récupérer les valeurs des champs :
      echo "$cfgpass" | cut -d "|" -f1 # Nom de l'utilisateur
      echo "$cfgpass" | cut -d "|" -f2 | md5sum # Ancien Mot de passe
      echo "$cfgpass" | cut -d "|" -f3 | md5sum # Nouveau Mot de passe
      echo "$cfgpass" | cut -d "|" -f4 | md5sum # Confirmation du nouveau mot de passe

      EXEMPLES

      Display a file selector with the title Select a file to remove. The file selected is returned on standard output.

      zenity --file-selection --title="Select a file"

       

      Display a text entry dialog with the title Select Host and the text Select the host you would like to flood-ping. The entered text is returned on standard output.

      zenity --entry --title "Select Host" --text "Select the host"

       

      The return code will be 0 (true) if OK is selected, and 1 (false) if Cancel is selected.

      zenity  --question --text "Ma super question"

       

      Search results in a list dialog with the title Search Results and the text Finding all header files….

      find . -name '*.h' | zenity --list --text "Choix:" --column "Files"

       

      Show a notification in the message tray

      zenity --notification --window-icon=update.png --text "System update necessary!"

       

      Display a weekly shopping list in a check list dialog with Apples and Oranges pre selected

      zenity --list --checklist --column "Buy" --column "Item" TRUE Apples TRUE Oranges FALSE Pears FALSE Toothpaste

       

      Display a progress dialog while searching for all the postscript files in your home directory

      find $HOME -name '*.ps' | zenity --progress --pulsate

      Méthode 2 pour récupérer la valeur

      dossierSortie=$(zenity --file-selection --text="Choissisez un dossier" --directory);
      echo ${dossierSortie}

      Méthode 3 pour récupérer la valeur

      variable=$(zenity --entry --title="Choix" --text="Indiquez un mot")
      echo "Mot choisi : "$variable

      Méthode pour récupérer la valeur avec --question

      zenity --question --title "coffee" --text "Faire du café ?"
      if [ $? = 0 ] ; then
         echo "OUI"
      else
         echo "NON"
      fi

      Voir aussi

      Page officielle de Zenity

      zenity --info et notify-send !

      Topic parlant des bonus

      Script de génération de fenêtre Zenity

 

Aucun commentaire

 

Laissez un commentaire