folk.zone Wiki

a public knowledge base

Site Tools


terminal:scripting:gui_scripting

GUI Scripting from the Shell

Give shell scripts dialogs and notifications without a real GUI app. notify-send and zenity are usually already installed; yad (zenity fork) is in the repos and much more capable for forms.

Notifications

notify-send "Backup done"                          # transient, doesn't steal focus
notify-send -u critical "Fan failure!"             # sticky/urgent
notify-send -i dialog-information "Hi" "body text"

From cron/timer scripts, notifications need the session bus:

export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus"
notify-send "timer fired"

zenity: simple dialogs

name=$(zenity --entry --text="Name:")              # text input
zenity --question --text="Continue?" && do_thing   # yes/no via exit code
zenity --info --text="Done."                       # OK box
zenity --error --text="It broke."
file=$(zenity --file-selection)                    # file picker
zenity --calendar                                  # date picker → prints date
zenity --password                                  # masked entry
zenity --list --column=A a b c                     # pick from list → prints
seq 100 | zenity --progress --auto-close           # progress bar from a pipe

Every dialog's exit code: 0 = OK/accept, 1 = cancel/closed. Capture output on stdout for input-type dialogs.

yad: when zenity isn't enough

sudo apt install yad
# multi-field form
yad --form --title="Add user" \
    --field="Name" --field="Email" --field="Admin:CHK" \
    "Ada" "ada@x" FALSE
# prints pipe-separated values: Ada|ada@x|TRUE|
 
# pick a file, show a text view, menu dialog…
yad --file --directory                      # dir picker
cat log.txt | yad --text-info --width=600 --height=400
yad --list --column=Pkg --column=Size pkg1 1M pkg2 2M

Choosing

  • Toast/status updates → notify-send
  • One input or a yes/no → zenity
  • A real form, multi-select list, or embedded widgets → yad
  • Terminal-only dialogs → dialog / whiptail (ncurses menus)

A small pattern

#!/usr/bin/env bash
# ask, act, report: all GUI
target=$(yad --entry --title="Backup" --text="Backup to which dir?" \
        --entry-text="/media/$USER/TRANSFER") || exit 0
if rsync -a ~/Documents/ "$target/Documents/"; then
  notify-send "Backup" "Done → $target"
else
  zenity --error --text="rsync failed: check disk space"
fi

Bind scripts like this to a hotkey in LXQt → Shortcut Keys, or drop a .desktop launcher in ~/.local/share/applications/ to get a menu entry.

See Also

terminal/scripting/gui_scripting.txt · Last modified: by 127.0.0.1