How To Create a Custom Linux App Menu With Zenity
Your own custom menu to get things done!
Creating GUI (Graphical User Interfaces) with any operating system boils down to Ncurses / Whiptail type GUIs for the terminal, or full blown GUIs on the desktop. We will be building the latter, but using the terminal to run commands. Zenity, a tool to display GTK dialog boxes from the terminal and shell scripts is a great tool and it is very easy to use.
In this how-to, we will create a simple application launcher using a Bash script. Selecting an application will trigger the terminal command and open the application. Leaving our app launcher ready to run another application.
Zenity is a fantastic tool for creating GUI applications via the terminal and the Zenity project has a full manual on how to use it.
For this project you will need any computer running a Linux operating system. We’ve chosen Ubuntu 24.04, but it will also work on Debian, Fedora or many other Linux distributions. We tested it on CrunchbangPlusPlus Linux and Zenity worked as expected.
Creating the custom application launcher
Our first step is to create the custom application launcher, and for this we will be using a text editor and writing Bash shell scripts. Bash is a great programming language to learn and it can expand what your operating system can do.
- Open a terminal and first update the software repository list, and then install zenity.
sudo apt update && sudo apt install zenity
- Open a text editor to write the code. We’re using Geany, but you are free to use any text editor. Ubuntu 24.04 uses the Gnome “Text Editor” by default, but you can also install Microsoft’s Visual Studio Code editor.
- Start the code by pointing the code to the Bash interpreter.
#!/bin/bash
- Create a loop that will repeat until the user closes the application.
while true; do
- Create a list of applications in the Zenity app. The application window should have a title (App menu) and be formatted into a column. Add as many application names as you need. The height and width can be tweaked later to match your requirements
CHOICE=$(zenity --list --title="App menu" \
--column="Apps" \
"Chrome" \
"Geany" \
"Thonny" \
"Inkscape" \
"GIMP" \
--height=400 --width=300)
- Add an if conditional check that will activate if the user exits the dialog.
if [ $? -ne 0 ]; then
break
fi
- Create a case statement (used to simplify long conditionals) as a long list to check the user’s input, stored in the CHOICE variable. If the choice matches an entry on the list, then the corresponding command is launched. If there are no matches (unlikely, but we use this just in case) then the error message is displayed.
case $CHOICE in
"Chrome")
google-chrome &
;;
"Geany")
geany &
;;
"Thonny")
thonny &
;;
"Inkscape")
inkscape &
;;
"GIMP")
gimp &
;;
*)
zenity --error --text="Invalid option, please try again."
;;
esac
- Close the main loop.
done
- Save the code as menu.sh to your home directory.
- Open a terminal window and set the file as executable. You can also make it executable via the File Manager. Right click >> Properties and set the Permissions to “Executable as a Program”.
chmod +x menu.sh
- Run the code in the terminal to test that it works.
./menu.sh
Complete Code Listing
#!/bin/bash
while true; do
CHOICE=$(zenity --list --title="App menu" \
--column="Apps" \
"Chrome" \
"Geany" \
"Thonny" \
"Inkscape" \
"GIMP" \
--height=400 --width=300)
if [ $? -ne 0 ]; then
break
fi
case $CHOICE in
"Chrome")
google-chrome &
;;
"Geany")
geany &
;;
"Thonny")
thonny &
;;
"Inkscape")
inkscape &
;;
"GIMP")
gimp &
;;
*)
zenity --error --text="Invalid option, please try again."
;;
esac
done
Run the code when Ubuntu Starts
Our code is written, but we have to manually start it when we need it. But we can set the menu to launch when the desktop loads, all we need to do is tell the OS where to find the file.
- Press the Windows / Super key and search for “startup app”. Select Startup Applications.
- Click on Add.
- Set the application name to App Launcher, and point the command to where menu.sh is (your home directory). Add a comment to identify what the command does. Click Add to save.
- Reboot and login, the app launcher should auto run and be ready for use.
Stay On the Cutting Edge: Get the Tom's Hardware Newsletter
Get Tom's Hardware's best news and in-depth reviews, straight to your inbox.
Les Pounder is an associate editor at Tom's Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program "Picademy".
-
pmpatrick For creating gui bash dialog boxes, most people have moved off zenity and have gone to yad instead. Yad is a fork of zenity which was created about 15 years ago. See:Reply
https://yad-guide.ingk.se/