{
    "title": "GNOME menu entries for Visual Studio Code projects",
    "slug": "gnome-menu-entries-for-visual-studio-code-projects",
    "excerpt": "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.",
    "body": "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](https://code.visualstudio.com/), my preferred code editor. I figured the quickest way to do this under [GNOME](https://www.gnome.org/) 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.\r\n\r\nTo 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.\r\n\r\nNext, 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.\r\n\r\nThe script:\r\n\r\n```\r\n#!/bin/bash\r\n\r\n# Define the base Projects directory\r\nPROJECTS_DIR=\"$HOME/Projects\"\r\n\r\n# Define the target application directory\r\nAPP_DIR=\"$HOME/.local/share/applications\"\r\nmkdir -p \"$APP_DIR\"\r\n\r\n# VS Code executable (adjust if necessary)\r\nVSCODE_EXECUTABLE=\"/usr/share/code/code\"\r\n\r\n# VS icon (adjust if necessary)\r\nICON_PATH=\"/usr/share/pixmaps/vscode.png\"\r\n\r\n# Find all .projectname files in subdirectories of PROJECTS_DIR\r\nfind \"$PROJECTS_DIR\" -type f -name \".projectname\" | while read -r project_file; do\r\n    # Get the parent directory of the .projectname file\r\n    project_dir=$(dirname \"$project_file\")\r\n\r\n    # Read the first line of the .projectname file as the project short name\r\n    project_short_name=$(head -n 1 \"$project_file\" | tr -d '[:space:]')\r\n\r\n    # Skip if the project short name is empty\r\n    if [ -z \"$project_short_name\" ]; then\r\n        echo \"Skipping: Empty short name in '$project_file'\"\r\n        continue\r\n    fi\r\n\r\n    # Define the desktop file path\r\n    desktop_file=\"$APP_DIR/vscode-$project_short_name.desktop\"\r\n\r\n    # Create the .desktop file\r\n    cat > \"$desktop_file\" <<EOL\r\n[Desktop Entry]\r\nType=Application\r\nName=Code $project_short_name\r\nExec=$VSCODE_EXECUTABLE \"$project_dir\"\r\nIcon=$ICON_PATH\r\nTerminal=false\r\nCategories=Development;IDE;\r\nEOL\r\n\r\n    # Make the file executable\r\n    chmod +x \"$desktop_file\"\r\n    echo \"Created: $desktop_file (opens $project_dir)\"\r\ndone\r\n\r\necho \"Done. You may need to refresh your desktop environment for changes to take effect.\"\r\n```\r\n\r\nWhenever I create a new project, I make sure to create the `.projectname` file for it, before rerunning the script.",
    "tags": [],
    "published_at": "2025-03-20 20:00:00",
    "url": "https://blog.philipnewborough.co.uk/posts/gnome-menu-entries-for-visual-studio-code-projects",
    "featured_image": "https://blog.philipnewborough.co.uk/media/og-80ce76d0-6ad2-404f-ac11-68e9b03c12bf.png"
}