Created
March 4, 2026 07:47
-
-
Save morgan9e/09a2a0a4650b5204c3ef2612bf3bd016 to your computer and use it in GitHub Desktop.
List all applications on XDG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| parse_desktop_file() { | |
| local file="$1" | |
| local section="" | |
| local mainexec="" | |
| local mainname="" | |
| while IFS= read -r line || [[ -n "$line" ]]; do | |
| [[ "$line" =~ ^[\;\#] || -z "$line" ]] && continue | |
| if [[ "$line" =~ ^\[(.+)\]$ ]]; then | |
| section="${BASH_REMATCH[1]}" | |
| continue | |
| fi | |
| if [[ "$section" == "Desktop Entry" && "$line" =~ ^([^=]+)=(.*)$ ]]; then | |
| key="${BASH_REMATCH[1]}" | |
| value="${BASH_REMATCH[2]}" | |
| [[ "$key" == "Name" ]] && mainname="$value" | |
| if [[ "$key" == "Exec" ]]; then | |
| mainexec="${value%% *}" | |
| if [[ "$mainexec" == "env" || "$mainexec" == "/usr/bin/env" ]]; then | |
| mainexec=$(echo "$value" | grep -oP '(^|[\s=])/?\w[\w/.-]+' | grep -v '=' | head -1 | xargs) | |
| fi | |
| fi | |
| fi | |
| done < "$file" | |
| jq -c -n --arg f "$file" --arg n "$mainname" --arg e "$mainexec" '{file: $f, name: $n, exec: $e}' | |
| # printf '%s\t%s\t%s\n' "$mainname" "$mainexec" "$file" | |
| } | |
| : "${XDG_DATA_HOME:=$HOME/.local/share}" | |
| : "${XDG_DATA_DIRS:=/usr/local/share:/usr/share}" | |
| IFS=':' read -r -a data_dirs <<< "$XDG_DATA_HOME:$XDG_DATA_DIRS" | |
| shopt -s nullglob | |
| for dir in "${data_dirs[@]}"; do | |
| app_dir="${dir%/}/applications" | |
| [[ -d "$app_dir" ]] || continue | |
| for desktop_file in "$app_dir"/*.desktop; do | |
| parse_desktop_file "$desktop_file" | |
| done | |
| done | |
| shopt -u nullglob |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment