GNOME menu entries for Visual Studio Code projects

I work on a large number of code projects and I wanted a quick way to open any of my projects in Visual Studio Code, my preferred code editor. I figured the quickest way to do this under GNOME would be to create a .desktop file for each project directory. The idea being, I could just hit the Super key to bring up GNOME's overview, before typing a few characters to open a project.

To achieve this, I first spent a little while creating a file named .projectname in the root of each project directory. The file simply contained a short name for the project, something like ProjectFoo. I could have automated this process, but I wanted to use a custom short name for each project, as opposed to using the directory name or some other readable value. Also, the GNOME overview only shows limited characters for each entry with long names being truncated, so a short name was a must.

Next, I created a shell script to automate the process of creating the .desktop files. All my projects live under ~/Projects, so the script does a recursive lookup to find all .projectname files and creates a .desktop file in ~/.local/share/applications for each one. The .desktop files are all identical apart from their Name and Exec values.

The script:

#!/bin/bash

# Define the base Projects directory
PROJECTS_DIR="$HOME/Projects"

# Define the target application directory
APP_DIR="$HOME/.local/share/applications"
mkdir -p "$APP_DIR"

# VS Code executable (adjust if necessary)
VSCODE_EXECUTABLE="/usr/share/code/code"

# VS icon (adjust if necessary)
ICON_PATH="/usr/share/pixmaps/vscode.png"

# Find all .projectname files in subdirectories of PROJECTS_DIR
find "$PROJECTS_DIR" -type f -name ".projectname" | while read -r project_file; do
    # Get the parent directory of the .projectname file
    project_dir=$(dirname "$project_file")

    # Read the first line of the .projectname file as the project short name
    project_short_name=$(head -n 1 "$project_file" | tr -d '[:space:]')

    # Skip if the project short name is empty
    if [ -z "$project_short_name" ]; then
        echo "Skipping: Empty short name in '$project_file'"
        continue
    fi

    # Define the desktop file path
    desktop_file="$APP_DIR/vscode-$project_short_name.desktop"

    # Create the .desktop file
    cat > "$desktop_file" <<EOL
[Desktop Entry]
Type=Application
Name=Code $project_short_name
Exec=$VSCODE_EXECUTABLE "$project_dir"
Icon=$ICON_PATH
Terminal=false
Categories=Development;IDE;
EOL

    # Make the file executable
    chmod +x "$desktop_file"
    echo "Created: $desktop_file (opens $project_dir)"
done

echo "Done. You may need to refresh your desktop environment for changes to take effect."

Whenever I create a new project, I make sure to create the .projectname file for it, before rerunning the script.

gnome linux vscode

Comments

New Comment

If you have a comment you'd like to share, feel free to leave it below. I moderate all comments before they are published. Markdown is enabled. See syntax for help.


I'll never share your email with anyone else.
Philip Newborough and a donkey enjoying a beer.

About

My name is and I’m a full stack web developer living and working in Lincoln, England. This website (philipnewborough.co.uk) serves as my personal homepage. When I’m not working with tech, I love to ride bicycles with my wife and friends.

An IndieWeb Webring 🕸💍