config

My configuration files and custom scripts.
git clone https://git.tronto.net/config
Download | Log | Files | Refs

open-file (2399B)


      1 #!/bin/sh
      2 
      3 # Inspired by https://github.com/salman-abedin/launch.sh
      4 # Launches files based on their mimetypes
      5 # Requires: dmenu_path or similar (fallback), dmenu-filepicker
      6 
      7 # Usage: open-file [-m menu] [-s launcher] [-t mimetype] [files...]
      8 
      9 menu="dmenu -i -l 15"
     10 
     11 # Change default apps here
     12 video=${VIDEO:-vlc}
     13 music=${MUSIC:-vlc}
     14 pdf=${PDF:-zathura}
     15 img="${IMGVIEW:-imv}"
     16 word="${WORDS:-libreoffice}"
     17 sheet="${SHEETS:-libreoffice}"
     18 html="${BROWSER:-firefox} --new-window"
     19 xedit="${XEDIT:-xedit}"
     20 
     21 usage() {
     22 	echo "Usage: open-file [-m MENU] [-s LAUNCHER] [-t MIMETYPE] [files...]"
     23 }
     24 
     25 openfile() {
     26 	f="$*"
     27 	localmime="$mimetype"
     28 
     29 	[ ! -f "$f" ] && echo "$f: bad argument" && exit 1
     30 	[ -z "$mimetype" ] && localmime="$(file --mime-type "$f" -bL)"
     31 
     32 	prog=""
     33 	case "$localmime" in
     34 		video/*)
     35 			prog="$video"
     36 			;;
     37 		audio/*)
     38 			prog="$music"
     39 			;;
     40 		application/pdf | application/postscript | \
     41 		image/vnd.djvu | application/epub*)
     42 			prog="$pdf"
     43 			;;
     44 		image/*)
     45 			prog="$img"
     46 			;;
     47 		application/msword | \
     48 		application/vnd.oasis.opendocument.text | text/rtf | \
     49 		application/vnd.openxmlformats-officedocument.wordprocessingml.*)
     50 			prog="$word"
     51 			;;
     52 		application/ms-excel | \
     53 		application/vnd.oasis.opendocument.spreadsheet | \
     54 		application/vnd.openxmlformats-officedocument.spreadsheetml.*)
     55 			prog="$sheet"
     56 			;;
     57 		text/html | text/enriched)
     58 			prog="$html"
     59 			;;
     60 		text/*)
     61 			prog="$xedit"
     62 			;;
     63 		application/zip)
     64 			prog="unzip"
     65 			;;
     66 		application/x-rar-compressed)
     67 			prog="unrar"
     68 			;;
     69 		application/x-archive | application/x-tar | \
     70 		application/x-bzip2 | application/gzip | application/x-lzip | \
     71 		application/x-lzma | application/x-xz | application/x-gtar)
     72 			prog="tar -tavf"
     73 			;;
     74 		application/java-archive)
     75 			prog="java -jar"
     76 			;;
     77 		*)
     78 			if [ "$menu" = dmenu ]; then
     79 				menuprompt="-p \"How to open $f? \""
     80 			else
     81 				menuprompt="--prompt=\"How to open $f? \""
     82 			fi
     83 			prog=$(dmenu_path | $menu "$menuprompt")
     84 			;;
     85 	esac
     86 
     87 	if [ -n "$prog" ]; then
     88 		$launcher $prog "$f"
     89 	else
     90 		exit 1
     91 	fi
     92 }
     93 
     94 while getopts "m:s:t:" opt; do
     95 	case "$opt" in
     96 		m)
     97 			menu="$OPTARG"
     98 			;;
     99 		s)
    100 			launcher="$OPTARG"
    101 			;;
    102 		t)
    103 			mimetype="$OPTARG"
    104 			;;
    105 		*)
    106 			usage
    107 			exit 1
    108 			;;
    109 	esac
    110 done
    111 
    112 shift $((OPTIND - 1))
    113 
    114 if [ -n "$1" ]; then
    115 	while [ -n "$1" ]; do
    116 		openfile "$1" &
    117 		shift
    118 	done
    119 else
    120 	dmenu-filepicker -m "$menu" | while read -r line; do
    121 		openfile "$line" &
    122 	done
    123 fi