The buttons of YAD
By Bob Mesibov, published 02/04/2017 in Tutorials
I use the YAD dialog program quite a lot in my shell scripts. Written and maintained by Victor Ananjevksy, YAD is easy to use and has dozens of handy options.
In previous Linux Rain articles demonstrating YAD (here, here and here), I used only the default YAD buttons, "Cancel" and "OK". Those are the two buttons you get if you don't include any button options in your YAD command, as here:
yad --text "Would you like to participate in a survey?"
A more sensible choice of buttons for this particular dialog would be "Yes" and "No". You can get those buttons by specifying each with a "--button" option:
yad --text "Would you like to participate in a survey?" \
--button=gtk-no:1 --button=gtk-yes:0
Button order and position
Want "Yes" to the left of "No"? The order of the buttons is determined by their order in the command:
yad --text "Would you like to participate in a survey?" \
--button=gtk-yes:0 --button=gtk-no:1
You can also move the buttons around at the bottom of the dialog box. The choices for the "--buttons-layout" option are center, edge, end, spread and start, with end being the default. Here's center:
yad --text "Would you like to participate in a survey?" \
--button=gtk-no:1 --button=gtk-yes:0 --buttons-layout=center
You can even have no buttons at all. This might be OK for a notification dialog, but you'd need to click on the window-closing "X" to close the dialog:
yad --text "Would you like to participate in a survey?" --no-buttons
Button text and icons
The "gtk-yes" and "gtk-no" instructions determine how the buttons are labelled. Those instructions refer YAD to the many stock GTK images, which on my system live in /usr/share/icons subdirectories. Here are more stock examples:
yad --button=gtk-print:1 --button=gtk-quit:1 --button=gtk-dialog-error:1
You can also write your own button text:
yad --text "Would you like to participate in a survey?" \
--button=gtk-no:1 --button="I'll think about it":1 --button=gtk-yes:0
To put your own icons on a button, precede the path to the icon with a "!", like this:
yad --button=" One!$HOME/.icons/s1.png":1 \
--button=" Two!$HOME/.icons/s2.png":1 \
--button=" Three!$HOME/.icons/s3.png":1
Note that this won't work as a command in a BASH terminal, because BASH will interpret the "!" as a history operator. To get it to work in a terminal, temporarily disable BASH history with set +H. Restore history with set -H.
You can also label a button with a shell variable:
foo=$(date +"%d %b %Y")
yad --text="Click on the date:" --button=gtk-cancel:1 --button="$foo":0
Multiple button outputs
Probably the most frequent use of YAD is in allowing the user to make a choice (a date, a file, a colour, a text string etc) and sending that choice to standard output. Whether that happens or not depends on the exit status of the YAD command.
If the user clicks the "Cancel" button, or any button assigned an exit status of 1 (as in "--button=gtk-no:1"), then YAD exits without sending anything to stdout. YAD also exits this way if the user presses "Esc" on the keyboard or closes YAD by clicking on the window's "X", although in this case the exit status is 252.
If the user clicks the "OK" button, or any button assigned an exit status of 0 (as in "--button=gtk-yes:0"), then YAD sends the choice to stdout. YAD also does this if the user presses "Enter" on the keyboard.
Other exit status values can be assigned to buttons, to allow other actions in a shell script. For example, suppose you have the three browsers Chromium, Firefox and Dillo on your system. The following script will allow you to launch the selected browser (or to open a new window in an existing session if that browser is already running). Pressing "Enter" will cause YAD to exit in this case.
#!/bin/bash
yad --text="Choose a browser to launch:" \
--button=gtk-cancel:1 \
--button="Chromium":2 \
--button="Firefox":3 \
--button="Dillo":4
foo=$?
[[ $foo -eq 1 ]] && exit 0
if [[ $foo -eq 2 ]]; then
chromium && exit 0
elif [[ $foo -eq 3 ]]; then
firefox && exit 0
else
dillo && exit 0
fi
Buttons and commands
It's possible to put a command in the button option instead of an exit status, like this:
yad --text="Choose a browser to launch:" \
--button=gtk-cancel:1 \
--button="Chromium":chromium \
--button="Firefox":firefox \
--button="Dillo":dillo
YAD's "form" dialog allows you to do this in another way, by stacking buttons one on top of the other. The trick is to use the "FBTN" (or "fbtn") field type:
yad --form --width=250 --text="Choose a browser to launch:" \
--field="Chromium":fbtn "chromium" \
--field="Firefox":fbtn "firefox" \
--field="Dillo":fbtn "dillo" \
--button=gtk-cancel:1
NOTE that in both these cases the YAD dialog doesn't exit after the browser-launching command is executed. The dialog window remains open until YAD is closed manually. For this reason the "buttons-with-exit-status-in-a-script" strategy is better if you're using buttons alone for making a choice.
...and lots more
The YAD man page is very good, but for more ideas on using YAD see this page from Puppy Linux enthusiast "smokey01".
Top image by OhWeh in Wikimedia Commons