Skip to content

Instantly share code, notes, and snippets.

@morgan9e
Created June 14, 2024 17:42
Show Gist options
  • Save morgan9e/653edddc3a7f51b587dc9d002ecce851 to your computer and use it in GitHub Desktop.
Save morgan9e/653edddc3a7f51b587dc9d002ecce851 to your computer and use it in GitHub Desktop.
#!/bin/bash
parse_desktop_file() {
local file="$1"
local section=""
local mainexec=""
local mainname=""
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" =~ ^\;.* || "$line" =~ ^#.* || -z "$line" ]]; then
continue
fi
if [[ "$line" =~ ^\[.*\]$ ]]; then
section="${line:1:-1}"
continue
fi
if [[ "$line" =~ ^([^=]+)=(.*)$ ]]; then
key="${BASH_REMATCH[1]}"
value="${BASH_REMATCH[2]}"
# echo "[$section] $key = $value"
if [ "$section" == "Desktop Entry" ]; then
if [ "$key" == "Exec" ]; then mainexec=$value; fi
if [ "$key" == "Name" ]; then mainname=$value; fi
fi
fi
done < "$file"
echo "{\"file\": \"$file\", \"name\": \"$mainname\", \"exec\": \"$mainexec\"}"
}
shopt -s nullglob
IFS=':' read -r -a paths <<< "$XDG_DATA_DIRS:/home/$USER/.local/share/"
for path in "${paths[@]}"; do
AP=$(realpath "$path/applications")
for appfile in $AP/*.desktop; do
parse_desktop_file $appfile;
done
done
shopt -u nullglob
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment