Skip to content

Instantly share code, notes, and snippets.

@jnbdz
Created March 2, 2024 03:17
Show Gist options
  • Save jnbdz/5a0f9bb3b3516206d39ceb4270402713 to your computer and use it in GitHub Desktop.
Save jnbdz/5a0f9bb3b3516206d39ceb4270402713 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Your system locale, for example "en_US". You might dynamically get this with `echo $LANG` or `locale`.
locale="en_US"
locale=${locale%%_*} # Extract just the first part (e.g., "en" from "en_US")
# Find .desktop files that can handle HTTP/HTTPS and are not mimeinfo.cache
desktop_files=$(grep -rE "x-scheme-handler/(http|https)" /usr/share/applications/ ~/.local/share/applications/ | grep -E ".desktop:" | awk -F ":" '{ print $1 }' | sort -u)
# Create an associative array to hold appName => Exec mapping
declare -A appsExecMap
# Process each .desktop file
while IFS= read -r file; do
# Use awk to read only from the [Desktop Entry] section
name=$(awk -F "=" -v locale="$locale" -v RS="\n" '/^\[Desktop Entry\]/ {flag=1; next} /^\[/ {flag=0} flag && /^Name(\['"$locale"'\])?=/' "$file" | head -n 1 | cut -d= -f2-)
exec=$(awk -F "=" -v RS="\n" '/^\[Desktop Entry\]/ {flag=1; next} /^\[/ {flag=0} flag && /^Exec=/' "$file" | head -n 1 | cut -d= -f2-)
# If name was found, add it and its exec command to the map
if [[ ! -z "$name" && ! -z "$exec" ]]; then
appsExecMap["$name"]="$exec"
fi
done <<< "$desktop_files"
# Use fzf to let the user select an application name, then output the corresponding Exec command
selected_app=$(printf "%s\n" "${!appsExecMap[@]}" | fzf)
# Output the Exec command for the selected application
echo "${appsExecMap[$selected_app]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment