Created
March 9, 2024 20:03
-
-
Save jnbdz/4b76e9d9207cbffbcefcb54f9e353d87 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env bash | |
# Check for dependencies | |
if ! command -v fzf &> /dev/null; then | |
echo "fzf is required but it's not installed. Exiting." | |
exit 1 | |
fi | |
locale="en_US" | |
locale=${locale%%_*} # Extract just the first part (e.g., "en" from "en_US") | |
input="$1" | |
# Determine if input is a URI with a scheme, otherwise get realpath or return an error | |
if [[ "$input" =~ ^(https?|ftp|mailto|tel|sip|sips|magnet|ssh|sftp|webcal|rtsp|ldap|xmpp|bitcoin|ethereum|litecoin|vnc|steam|geo): ]]; then | |
uri_scheme="${BASH_REMATCH[1]}" | |
mime_type="x-scheme-handler/$uri_scheme" | |
elif [[ -e "$input" ]]; then | |
file_path=$(realpath "$input") | |
if [[ -d "$file_path" ]]; then | |
mime_type="inode/directory" | |
else | |
mime_type=$(xdg-mime query filetype "$file_path") | |
fi | |
else | |
echo "Invalid input or path does not exist: ${input}" | |
exit 1 | |
fi | |
# Function to parse .desktop files | |
parse_desktop_files() { | |
local search_path mime_filter="$1" | |
declare -A appsExecMap | |
if [[ -n "$mime_filter" ]]; then | |
search_path=$(grep -rls "^MimeType=.*$mime_filter" /usr/share/applications/ ~/.local/share/applications/) | |
else | |
search_path=$(find /usr/share/applications/ ~/.local/share/applications/ -name "*.desktop") | |
fi | |
while IFS= read -r file; do | |
local 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-) | |
local exec=$(awk -F "=" -v RS="\n" '/^\[Desktop Entry\]/ {flag=1; next} /^\[/ {flag=0} flag && /^Exec=/' "$file" | head -n 1 | cut -d= -f2-) | |
if [[ -n "$name" && -n "$exec" ]]; then | |
appsExecMap["$name"]="$exec" | |
fi | |
done <<< "$search_path" | |
for name in "${!appsExecMap[@]}"; do | |
printf "%s\n" "$name" | |
done | |
} | |
compatible_apps=$(parse_desktop_files "$mime_type") | |
other_option="Other application" | |
selected_app=$(printf "%s\n%s" "$compatible_apps" "$other_option" | fzf --prompt="Select an application: ") | |
if [[ "$selected_app" == "Other application" ]]; then | |
all_apps=$(parse_desktop_files) | |
selected_app=$(printf "%s" "$all_apps" | fzf --prompt="Select an application: ") | |
fi | |
if [[ "$selected_app" != "Other application" && -n "$selected_app" ]]; then | |
exec_command=$(grep -rls "^Name=$selected_app" /usr/share/applications/ ~/.local/share/applications/ | xargs grep -hs "^Exec=" | head -n 1 | cut -d "=" -f2-) | |
# Adjust command placeholders | |
exec_command=${exec_command//%U/"'$input'"} | |
exec_command=${exec_command//%u/"'$input'"} | |
exec_command=${exec_command//%F/"'$file_path'"} | |
exec_command=${exec_command//%f/"'$file_path'"} | |
exec_command=${exec_command//%i/""} | |
exec_command=${exec_command//%c/""} | |
exec_command=${exec_command//%k/""} | |
exec_command=${exec_command//%v/""} | |
echo "Executing: $exec_command" | |
eval "$exec_command" | |
else | |
echo "No application selected or found for execution. Exiting." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment