-
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 :
--calendarCalendrier--entrySaisie de caractères--errorErreur à l’écran--file-selectionSélection d’un fichier--infoInformation--listListe--notificationNotification dans la zone prévue à cet effet--progressProgression--questionQuestion ( OUI / NON )--text-infoDu texte dans une fenêtre--warningAvertissement--scaleChoisir une valeur numérique à l’aide d’un curseur--formsFormulaire--passwordDemande un mot de passe (peut être utilisé avec ‘sudo -S’)--color-selectionSélection de couleurPour 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=TITRESet the dialog title--window-icon=CHEMIN/VERS/ICONEIcone avec le chemin. Une des quatres stock icons can be used: ‘error’, ‘info’, ‘question’ or ‘warning’--width=XLargeur en X pixels--height=XHauteur en X pixels--timeoutDélai d’expiration de la boîte de dialogue en secondesCalendrier
--text=STRINGSet the dialog text--day=INTRenseigner le jour--month=INTSet the calendar month--year=INTSet the calendar year--date-format=PATTERNFormat du retour de date. Exemple:%A %d/%m/%ySaisie 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=STRINGSet the dialog text--entry-text=STRINGEntrée par défaut--hide-textHide the entry textErreur (error)
--text=STRINGSet the dialog text--no-wrapDo not enable text wrapping--no-markupDo not enable pango markupSélecteur de fichier (file-selection)
--filename=FILENAMEDossier to be selected by default--multipleAllow selection of multiple filenames in file selection dialog--directoryActivate directory-only selection--saveActive save mode--separator=SEPARATORSpecify separator character when returning multiple filenames--confirm-overwriteConfirm file selection if filename already exists--file-filter=NAME | PATTERN1 PATTERN2Sets a filename filterInfo
--text=STRINGSet the dialog text--no-wrapDo not enable text wrapping--no-markupDo not enable pango markupListe
--text=STRINGSet the dialog text--column=STRINGSet the column header--checklistUse check boxes for first column--radiolistUse radio buttons for first column--separator=STRINGSet output separator character--multipleAllow multiple rows to be selected--editableAllow changes to text--print-column=NUMBERSpecify 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=NUMBERHide a specific column--hide-headerHides the column headerscheckbox
#!/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=STRINGSet the notification text--listenListen 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=textTexte de la boîte de dialogue--percentage=NbreSpécifier le pourcentage initial--pulsateBarre de progression qui pulse jusqu’à ce qu’un caractère EOF soit lu sur l’entrée standard.--auto-closeFermeture auto dès que 100% est atteint--auto-killTuer le processus parent si le bouton cancel est pressé--no-cancelMasquer le bouton cancelSi 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=STRINGSet the dialog text--no-wrapDo not enable text wrapping--no-markupDo not enable pango markup--ok-labelSet the text of the OK button--cancel-labelSet the text of the cancel buttonTexte
--filename=FILENAMEOpen file--editableAllow changes to text--checkbox=TEXTEnable a checkbox for use like a ‘I read and accept the terms.’--ok-labelTexte du bouton OK--cancel-labelTexte du bouton cancelAlerte (warning)
--text=STRINGSet the dialog text--no-wrapDo not enable text wrapping--no-markupDo not enable pango markupValeur numérique avec curseur
--text=STRINGSet the dialog text--value=VALUESet initial value--min-value=VALUESet minimum value--max-value=VALUESet maximum value--step=VALUESet step size--print-partialPrint partial values--hide-valueHide valueSélection de couleur
--color=VALUESet the initial color--show-paletteShow the palettePassword
--usernameDisplay the username fieldFormulaire
--text=TexteSpécifier le texte affiché dans la boîte de dialogue de barre de progression.--separator=SÉPARATEURDéfinit le caractère séparateur de sortie.--add-entry=Nomdu 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ÈLEDé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
zenity --info et
notify-send!