Customize your system tray with YAD
Bespoke System Tray
![© Photo by Suhyeon Choi on Unsplash © Photo by Suhyeon Choi on Unsplash](/var/linux_magazin/storage/images/issues/2023/268/bash-tray-scripts/photobysuhyeonchoionunsplash_tray.png/817343-1-eng-US/PhotobySuhyeonChoionUnsplash_Tray.png_medium.png)
© Photo by Suhyeon Choi on Unsplash
YAD lets you customize your system tray with one-line Bash tray scripts.
My goal was to find a simple way to group together my favorite apps and web pages into a system tray item. There are a number of different approaches to this, but for my requirements I found that the YAD (Yet Another Dialog) tool [1] gave me everything that I needed, and I could do it all with just one line of Bash script.
In this article, I will introduce Bash tray scripts with three examples. The first example will show how to create tray scripts that put Linux diagnostic data into both custom dialogs and terminal windows. In the second example, I will add pop-up browser windows to a right-click submenu. The final example will look at how to toggle the tray icon, command, and tooltip with simulated weather conditions.
Getting Started
There are a number of different tools for creating system tray applications, such as AllTray and KDocker. For my projects, I prefer YAD because it lets you create custom dialogs and it supports dynamic changes to the tray features. YAD is a command-line dialog tool very similar to Zenity [2], but with some added features such as system tray support and a more complete dialog functionality.
For Debian/Raspian/Ubuntu systems, you can install YAD with:
sudo apt install yad
Creating a Tray Script
Listing 1 is a one-line Bash script that calls YAD to create a system tray item (Figure 1). Because Bash and YAD statements can be quite long, you can make the code more readable by using backslash (\
) characters to extend a statement across multiple lines, as shown in Listing 1.
Listing 1
Creating a System Tray Item
yad --notification --image="gtk-execute" \ --command="yad --text=\"$(lsusb)\" \ --title=\"USB Info\" --fixed --button=ok" \ --text="My Fav Utility"
![](/var/linux_magazin/storage/images/issues/2023/268/bash-tray-scripts/figure-1/817346-1-eng-US/Figure-1_large.png)
The yad
statement in Listing 1 uses the --notification
and --image
options to put a user-defined icon in the system tray. The --command
option calls an application or script when you click on the tray item. The --text option defines tooltips.
In Figure 1, the command-line lsusb
utility lists the USB-connected devices. The output from lsusb
is shown in a YAD text dialog.
To terminate a YAD tray script, use a center mouse click. You can also terminate from a right-click menu, which I'll discuss later.
YAD includes a handy yad-icon-browser
tool, which lets you review available icons (Figure 2). To make coding a little easier, YAD only needs the image name; a full path to the image is not required.
Many applications, such as the top
system performance tool, are best viewed in a terminal window. The YAD tray script in Listing 2 calls top
within a terminal window (Figure 3).
Listing 2
Calling top in a Terminal Window
yad --notification --image="media-memory" \ --command="xterm -hold -fa Monospace -fs 12 \ -T 'System Performance' -e 'top' " \ --text="Show System Performance"
Listing 2 calls xterm
to launch a terminal window. The -e
option executes the top
utility. The -hold
option keeps the terminal open after the command is complete. Font types and sizes can be defined with the -fa
and -fs
options. The terminal window caption is set using the -T
parameter.
Right-Click Menus
YAD also supports right-click menu command items in addition to left-click commands. The syntax for menu items is:
--menu="menu_title1 ! menu_command1 | menu_title2 ! menu_command2 ... "
Listing 3 shows the main command and then four right-click menu options (Figure 4). The main command calls iostat
(the CPU and I/O reporting utility). The first three right-click options are vmstat
(memory stats), lm-sensor
(print sensor info), and lsusb
(list USB devices). The fourth option kills the script.
Listing 3
Adding Four Right-Click Menu Options
#!/bin/bash # # tray2yad.sh - create tray item with some Bash utilities # - add a function to change YAD text options # nice_dialog() { # Use Pango markup language for custom text presentations echo "yad --text='<tt>$($1)</tt>' \ --title=$2 --button=OK \ --fixed " } # Create a system tray item with 4 right click items yad --notification --image="applications-utilities" \ --command="$(nice_dialog 'iostat -c' 'IOSTAT')" \ --menu="Memory! $(nice_dialog 'vmstat' 'VMSTAT') \ | Sensors! $(nice_dialog 'sensors' 'SENSORS') \ | USB ! $(nice_dialog 'lsusb' 'USB') \ | Quit ! killall yad" \ --text="My Fav Utilities"
Instead of using one extremely large Bash statement, the nice_dialog
function is created to make the output a little more presentable. This function adjusts font type and font size for text within the YAD dialog. For more information on how to configure YAD color and font options, see the Pango markup language [3] documentation.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.
-
SUSE Offers CentOS 7 Support with Liberty Linux Lite
SUSE's Liberty Linux support offering now includes CentOS 7, which means businesses won't be forced to migrate those servers for some time.
-
Ubuntu's App Center Finally Supports Local Installs Again
If you regularly download .deb files and would prefer a GUI method of installing, Ubuntu has your back.
-
AlmaLinux Now Supports Raspberry Pi 5
If you're looking to create with the Raspberry Pi 5 and want to use AlmaLinux as your OS, you're in luck because it's now possible.
-
Kubuntu Focus Releases New Iterations of Ir14 and Ir16 Laptops
If you're a fan of the Kubuntu Focus laptops or have been waiting for the right time to purchase one, that time might be now.
-
NixOS 24.05 Is Ready for Prime Time
The latest release of NixOS (Uakari) has arrived and offers its usual reproducible, declarative, and reliable goodness.
-
Linux Lite 7.0 Officially Released
Based on Ubuntu 24.04 and kernel 6.8, Linux Lite version 7 now offers more options than ever.
-
KaOS Linux 2024.05 Adds Bcachfs Support and More
With updates all around, KaOS Linux now includes support for the bcachefs file system.
-
TUXEDO Computers Unveils New Iteration of the Stellaris Laptop Line
The Stellaris Slim 15 is the 6th generation and includes either an AMD or Intel CPU