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)