xdgdefault (1387B)
1 #!/usr/bin/env sh 2 3 # Description: Sets the xdg-open's default application for the current entry's file 4 # type. ${XDG_DATA_DIRS} and ${XDG_DATA_HOME} are set to the recommended 5 # defaults if unset, as specified in XDG Base Directory Specification 6 # - http://specifications.freedesktop.org/basedir-spec/. 7 # 8 # Dependencies: xdg-utils, fzf or dmenu (GUI) 9 # 10 # Shell: POSIX compliant 11 # Author: lwnctd 12 13 # set to 1 to enable GUI apps 14 GUI="${GUI:-0}" 15 16 if [ "$GUI" -ne 0 ] && command -v dmenu > /dev/null 2>& 1; then 17 menu="dmenu -i -l 7" 18 elif command -v fzf > /dev/null 2>& 1; then 19 menu="fzf -e --tiebreak=begin" 20 fi 21 22 if [ -z "$1" ] || [ -z "$menu" ] > /dev/null 2>& 1; then 23 exit 1 24 fi 25 26 ftype=$(xdg-mime query filetype "$2/$1") 27 28 if [ -z "$ftype" ]; then 29 exit 1 30 fi 31 32 dirs=${XDG_DATA_DIRS:-/usr/local/share:/usr/share} 33 dirs=${dirs}:${XDG_DATA_HOME:-$HOME/.local/share}: 34 35 while [ -n "$dirs" ]; do 36 d=${dirs%%:*} 37 if [ -n "$d" ] && [ -d "$d"/applications ]; then 38 set -- "$@" "$d"/applications 39 fi 40 dirs=${dirs#*:} 41 done 42 43 app=$(find "$@" -iname '*.desktop' -exec grep '^Name=' {} + \ 44 | sort -u -t ':' -k 1,1 \ 45 | sed -E 's;.+/(.+desktop):Name=(.+);\2:\1;' \ 46 | sort -t ':' -k 1,1 \ 47 | column -t -s ':' -o "$(printf '\t')" \ 48 | $menu \ 49 | cut -f 2) 50 51 if [ -n "$app" ]; then 52 xdg-mime default "$app" "$ftype" 53 fi