Creating Custom Scripts in Rofi for VSCode Project Management
Learn how to create custom scripts in Rofi for managing VSCode projects using tags, and automate project selection and opening.
If you don't know what Rofi is and how to configure it, please check my previous blog post: Rofi Configuration.
Additionally, if you're new to shell scripting and want to learn how to write your own scripts, check out this detailed guide on Mastering Bash Scripting: A Practical Guide.
In this blog post, I'll walk you through creating a custom script in Rofi to manage and open VSCode projects by tag, using the vscode-project-manager.sh
script as an example.
Prerequisites
Before we start, you'll need to install the VSCode Project Manager plugin. You can find it in the VSCode marketplace here. This plugin helps you organize and quickly open projects in VSCode.
Once you have the plugin installed, follow these steps to create the script.
The Script: vscode-project-manager.sh
What the Script Does
This script allows you to easily select and open VSCode projects based on tags. It fetches a list of projects from the projects.json
file used by the VSCode Project Manager plugin, then lets you pick a tag, and finally open the project associated with that tag.
Step-by-Step Breakdown
We begin by reading the projects.json
file, which contains your project information. The file is usually located at $HOME/.config/Code/User/globalStorage/alefragnani.project-manager/projects.json
. If the file doesn't exist, we exit since there is no reason to continue.
# Path to the JSON file where VSCode stores the project information JSON_FILE="$HOME/.config/Code/User/globalStorage/alefragnani.project-manager/projects.json" # Check if the JSON file exists if [ ! -f "$JSON_FILE" ]; then echo "Error: The Project Manager JSON file does not exist at $JSON_FILE" exit 1 fi
The
projects.json
file is typically stored in$HOME/.config/Code/User/globalStorage/alefragnani.project-manager/
unless you have changed it in settings. You can confirm this by opening the Project Manager in VSCode, clicking the edit icon, and it will open theprojects.json
file.
Next, we start extracting the tags from the projects.json
. If we have any tags, we open a menu with all available tags.
# Fetch all unique tags from the projects.json file TAGS=$(jq -r '.[] | select(.enabled == true) | .tags[]' "$JSON_FILE" | sort | uniq) # If tags are found, show available tags in a Rofi menu and capture the selected tag if [ -n "$TAGS" ]; then SELECTED_TAG=$(echo "$TAGS" | rofi -dmenu -p "Select a tag:") # If the user cancels or selects nothing, exit the script if [ -z "$SELECTED_TAG" ]; then echo "No tag selected. Exiting..." exit 0 fi fi
Now, we check if a tag has been selected. If so, we filter projects based on the selected tag, otherwise, we display all available projects.
# If no tag selected or the selected tag is empty, fetch all projects if [ -z "$SELECTED_TAG" ]; then PROJECTS=$(jq -r '.[] | select(.enabled == true) | .name' "$JSON_FILE") else # Get projects related to the selected tag PROJECTS=$(jq -r ".[] | select(.enabled == true) | select(.tags | index(\"$SELECTED_TAG\")) | .name" "$JSON_FILE") fi
If no projects are found, we exit the script. Otherwise, we open a menu to let the user select a project.
# If no projects are found, exit the script if [ -z "$PROJECTS" ]; then echo "No projects found for the selected tag." exit 1 fi # Show available projects in a Rofi menu and capture the selected project SELECTED_PROJECT=$(echo "$PROJECTS" | rofi -dmenu -p "Select a project to open:")
Next, we check if a project has been selected. If not, we exit the script, as there's no reason to continue.
# If the user cancels or selects nothing, exit the script if [ -z "$SELECTED_PROJECT" ]; then echo "No project selected. Exiting..." exit 0 fi
Finally, we try to read the rootPath
for the selected project. If the rootPath
exists, we open it with Visual Studio Code
. Otherwise, we print an error and exit the script.
# Get the rootPath for the selected project ROOT_PATH=$(jq -r ".[] | select(.name == \"$SELECTED_PROJECT\") | .rootPath" "$JSON_FILE") # Check if the rootPath exists, then open it in VSCode if [ -d "$ROOT_PATH" ]; then code "$ROOT_PATH" else echo "Error: Root path for project '$SELECTED_PROJECT' does not exist at $ROOT_PATH." exit 1 fi
Example Script
Here’s the example script that you can use:
# Path to the JSON file where VSCode stores the project information JSON_FILE="$HOME/.config/Code/User/globalStorage/alefragnani.project-manager/projects.json" # Check if the JSON file exists if [ ! -f "$JSON_FILE" ]; then echo "Error: The Project Manager JSON file does not exist at $JSON_FILE" exit 1 fi # Fetch all unique tags from the projects.json file TAGS=$(jq -r '.[] | select(.enabled == true) | .tags[]' "$JSON_FILE" | sort | uniq) # If tags are found, show available tags in a Rofi menu and capture the selected tag if [ -n "$TAGS" ]; then SELECTED_TAG=$(echo "$TAGS" | rofi -dmenu -p "Select a tag:") # If the user cancels or selects nothing, exit the script if [ -z "$SELECTED_TAG" ]; then echo "No tag selected. Exiting..." exit 0 fi fi # If no tag selected or the selected tag is empty, fetch all projects if [ -z "$SELECTED_TAG" ]; then PROJECTS=$(jq -r '.[] | select(.enabled == true) | .name' "$JSON_FILE") else # Get projects related to the selected tag PROJECTS=$(jq -r ".[] | select(.enabled == true) | select(.tags | index(\"$SELECTED_TAG\")) | .name" "$JSON_FILE") fi # If no projects are found, exit the script if [ -z "$PROJECTS" ]; then echo "No projects found for the selected tag." exit 1 fi # Show available projects in a Rofi menu and capture the selected project SELECTED_PROJECT=$(echo "$PROJECTS" | rofi -dmenu -p "Select a project to open:") # If the user cancels or selects nothing, exit the script if [ -z "$SELECTED_PROJECT" ]; then echo "No project selected. Exiting..." exit 0 fi # Get the rootPath for the selected project ROOT_PATH=$(jq -r ".[] | select(.name == \"$SELECTED_PROJECT\") | .rootPath" "$JSON_FILE") # Check if the rootPath exists, then open it in VSCode if [ -d "$ROOT_PATH" ]; then code "$ROOT_PATH" else echo "Error: Root path for project '$SELECTED_PROJECT' does not exist at $ROOT_PATH." exit 1 fi
How It Will Look in Action
Once you run the script, this will open Rofi, allowing you to search and select the tags (if they exist), then open the projects and select the project and open it in VSCode.
![]() |
---|
vscode project-manager menu. |
Conclusion
In this blog post, we've created a custom script for Rofi that interacts with VSCode and allows you to quickly open projects based on tags. You can further enhance and customize this script as per your needs.
The complete vscode-project-manager.sh
script and other useful scripts are available in this repository. Feel free to explore and contribute!