How to create a Custom Linux App Menu with Zenity

Configurare noua (How To)

Situatie

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.

Solutie

Pasi de urmat

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

Tip solutie

Permanent

Voteaza

(5 din 12 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?