Jump to content

How To Create a Custom Linux App Menu With Zenity


Recommended Posts

  • Author

How To Create a Custom Linux App Menu With Zenity

How To Create a Custom Linux App Menu With Zenity

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.

data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==

(Image credit: Tom’s Hardware)

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

data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==

(Image credit: Tom’s Hardware)

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.

  1. Open a terminal and first update the software repository list, and then install zenity.
sudo apt update && sudo apt install zenity
  1. 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.
  2. Start the code by pointing the code to the Bash interpreter.
#!/bin/bashba
  1. Create a loop that will repeat until the user closes the application.
while true; do
  1. 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)
  1. Add an if conditional check that will activate if the user exits the dialog.
    if [ $? -ne 0 ]; then
        break
    fi
  1. 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
  1. Close the main loop.
done
  1. Save the code as menu.sh to your home directory.
  2. 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
  1. 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.

  1. Press the Windows / Super key and search for “startup app”. Select Startup Applications.
  1. Click on Add.
  1. 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.
  1. Reboot and login, the app launcher should auto run and be ready for use.



Source link

#Create #Custom #Linux #App #Menu #Zenity

📬Pelican News

Source Link

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

Cookie Consent & Terms We use cookies to enhance your experience on our site. By continuing to browse our website, you agree to our use of cookies as outlined in our We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.. Please review our Terms of Use, Privacy Policy, and Guidelines for more information.