Advertisement
Guest User

ugly xdg

a guest
Nov 16th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.31 KB | None | 0 0
  1. desktop-files() {
  2.   filetype="$1"
  3.   default="$2"
  4.   apps=$(mimeo -d "$filetype" --show-all | sed "1d;/${default}/d")
  5.   echo -e "$apps"
  6. }
  7.  
  8. get-command() {
  9.   app="$1"
  10.   file=$(echo "${*:2}" | sed 's=\/=\\\/=g;s=\ =\\ =g')
  11.   exe=$(mimeo --desk2field "Exec" | grep -A 1 "$app" | sed 1d)
  12.   # TODO catch more cases than %U, %u, %f, %F ?
  13.   if [[ "$exe" =~ ^.*\%U.*$ ]]; then
  14.     exe=${exe//\%U/${file}}
  15.   elif [[ "$exe" =~ ^.*\%u.*$ ]]; then
  16.     exe=${exe//\%u/${file}}
  17.   elif [[ "$exe" =~ ^.*\%f.*$ ]]; then
  18.     exe=${exe//\%f/${file}}
  19.   elif [[ "$exe" =~ ^.*\%F.*$ ]]; then
  20.     exe=${exe//\%F/${file}}
  21.   else
  22.     exe="${exe} $file"
  23.   fi
  24.   echo "$exe"
  25. }
  26.  
  27. open-with() {
  28.   file="$1"
  29.   filetype=$(xdg-mime query filetype "$file")
  30.   default_app=$(xdg-mime query default "$filetype")
  31.   other_apps=$(desktop-files "$filetype" "$default_app")
  32.   binaries=$(print -rl -- "${(ko)commands}")
  33.   if [ ! "" = "$other_apps" ]; then
  34.     selection="${default_app} ${other_apps} ${binaries}"
  35.   else
  36.     selection="${default_app} ${binaries}"
  37.   fi
  38.   choice=$(echo -e "$selection" \
  39.              | tr ' ' '\n' \
  40.              | sed '/^$/d' \
  41.              | rofi -dmenu \
  42.                     -mesg "open ${file//\/\//\/}")
  43.   if [ ! "$?" = "0" ]; then
  44.     exit 1
  45.   fi
  46.   cmd=$(get-command "$choice" "$file")
  47.   eval "$cmd"
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement