Customize your system tray with YAD
Pop-Up Browser Windows
Midori [4], a lightweight web browser, has been available with the Raspberry Pi desktop for many years. One of Midori's advantages is that it can be easily launched as a pop-up window without affecting your main browser settings.
To install Midori on a Debian/Raspian/Ubuntu system enter:
sudo apt install midori
Midori can be run as a pop-up window by using the web application -a
command-line option.
Listing 4 shows a one-line Bash statement that creates a tray icon that can launch four different Midori browser windows (Figure 5).
Listing 4
Launching Midori Browser Windows
yad --notification --image="emblem-web" \ --command="midori -a https://news.google.com/" \ --menu="Facebook! midori -a https://mbasic.facebook.com \ | Linux! midori -a https://www.linux-magazine.com \ | Weather! midori -a https://www.theweathernetwork.com \ | Quit ! killall yad" \ --text="My Fav Web Pages"
Toggling
YAD supports the ability to dynamically change a tray item's icon, command, menuing, and tooltip. To do this you need to enable listen mode (--listen
) and then redirect the standard I/O (stdio) – file 1
as shown in Listing 5 – to a named pipe. After completion, a YAD tray item can be manipulated from the main script, an external script, or manually from a terminal.
Listing 5
Toggle Tray Item Features
01 #!/bin/bash 02 # 03 # tray_toggle.sh - toggle system tray items 04 # - create a named pipe for inputs 05 # 06 mytraypipe="/tmp/tray1.pipe" 07 08 # Make the named pipe (if it doesn't exist) 09 if ! test -e "$mytraypipe"; then 10 mkfifo $mytraypipe 11 fi 12 13 # redirect the stdio (file 1) to the named pipe 14 exec 1<> $mytraypipe 15 16 # create the notification icon 17 yad --notification \ 18 --listen \ 19 --image="emblem-colors-grey" \ \ 20 --text="My Tray Test" \ 21 --command="yad --text='Test Tray App' " <&1 22 23 # Every 10 seconds toggle the tray features with fake weather 24 while : 25 do 26 sleep 10 27 echo "action:yad --text='Rain until morning'" > $mytraypipe 28 sleep 1 29 echo "icon:stock_weather-showers" >> $mytraypipe 30 sleep 1 31 echo "tooltip:Rain until morning" >> $mytraypipe 32 sleep 10 33 echo "action:yad --text='Sunny for 2 days'" > $mytraypipe 34 sleep 1 35 echo "icon:stock_weather-sunny" >> $mytraypipe 36 sleep 1 37 echo "tooltip:Sunny for 2 days" >> $mytraypipe 38 done
Listing 5 shows a standalone script that creates a system tray item that can toggle some of its tray features (e.g., icon, tooltip, or command) every 10 seconds.
The first step in Listing 5 sets up a named pipe (lines 6 and 9-11). The named pipe is a file that is used to pass command arguments to YAD. Next, redirect stdio (file 1
) to the named pipe (line 14) and then have YAD get its input from the redirected file 1
(line 21).
A while
loop cycles every 10 seconds (line 24-26). Custom YAD command arguments are written/redirected to the named pipe with echo
statements (e.g., line 27). Adding sleep
statements helps ensure that YAD doesn't miss a command before a new command is written.
Listing 5 uses static weather data (Figure 6). A future step would be to periodically scan for actual weather data. If you're interested in using Bash to scrape the web [5], take a look at the Lynx [6] command-line browser. With one line of Bash, you can use Lynx to cleanly extract the contents of a web page; then, with a piped grep
command, you can parse out the desired data.
It should be noted that the YAD menus can also be dynamically changed. In Listing 5, to add a new menu item or change a menu item, the code would look something like:
echo "action:menu= \ menu_title1 ! menu_command1 | \ menu_title2 ! menu_command2" \ > $mytraypipe
Summary
Coding errors are a fact of life, so I found the command:
killall yad ; killall bash
to be quite useful, especially when I was playing with YAD in listening mode.
It's pretty amazing that with just one or a couple of lines of Bash script you can pull together all your favorite apps and web pages into a common system tray icon.
Infos
- YAD: https://www.systutorials.com/docs/linux/man/1-yad/
- Zenity: https://help.gnome.org/users/zenity/stable/
- Pango documentation: https://docs.gtk.org/Pango/pango_markup.html#pango-markup
- Midori: https://linuxcommandlibrary.com/man/midori
- "Simple Web Scraping with Bash" by Pete Metcalfe, Linux Magazine, issue 262, September 2022, p.36
- Lynx: https://lynx.invisible-island.net//
« Previous 1 2
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.
News
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.