scripts

Various scripts for UNIX-like systems
git clone https://git.tronto.net/scripts
Download | Log | Files | Refs | README

open-file (2298B)


      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="vlc"
     13 music="vlc"
     14 pdf="zathura"
     15 img="imv"
     16 word="libreoffice"
     17 sheet="libreoffice"
     18 html="firefox --new-window"
     19 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 | image/vnd.djvu)
     41 			prog="$pdf"
     42 			;;
     43 		image/*)
     44 			prog="$img"
     45 			;;
     46 		application/msword | \
     47 		application/vnd.oasis.opendocument.text | text/rtf | \
     48 		application/vnd.openxmlformats-officedocument.wordprocessingml.*)
     49 			prog="$word"
     50 			;;
     51 		application/ms-excel | \
     52 		application/vnd.oasis.opendocument.spreadsheet | \
     53 		application/vnd.openxmlformats-officedocument.spreadsheetml.*)
     54 			prog="$sheet"
     55 			;;
     56 		text/html | text/enriched)
     57 			prog="$html"
     58 			;;
     59 		text/*)
     60 			prog="$xedit"
     61 			;;
     62 		application/zip)
     63 			prog="unzip"
     64 			;;
     65 		application/x-rar-compressed)
     66 			prog="unrar"
     67 			;;
     68 		application/x-archive | application/x-tar | \
     69 		application/x-bzip2 | application/gzip | application/x-lzip | \
     70 		application/x-lzma | application/x-xz | application/x-gtar)
     71 			prog="tar -tavf"
     72 			;;
     73 		application/java-archive)
     74 			prog="java -jar"
     75 			;;
     76 		*)
     77 			if [ "$menu" = dmenu ]; then
     78 				menuprompt="-p \"How to open $f? \""
     79 			else
     80 				menuprompt="--prompt=\"How to open $f? \""
     81 			fi
     82 			prog=$(dmenu_path | $menu "$menuprompt")
     83 			;;
     84 	esac
     85 
     86 	if [ -n "$prog" ]; then
     87 		$launcher $prog "$f"
     88 	else
     89 		exit 1
     90 	fi
     91 }
     92 
     93 while getopts "m:s:t:" opt; do
     94 	case "$opt" in
     95 		m)
     96 			menu="$OPTARG"
     97 			;;
     98 		s)
     99 			launcher="$OPTARG"
    100 			;;
    101 		t)
    102 			mimetype="$OPTARG"
    103 			;;
    104 		*)
    105 			usage
    106 			exit 1
    107 			;;
    108 	esac
    109 done
    110 
    111 shift $((OPTIND - 1))
    112 
    113 if [ -n "$1" ]; then
    114 	while [ -n "$1" ]; do
    115 		openfile "$1" &
    116 		shift
    117 	done
    118 else
    119 	dmenu-filepicker -m "$menu" | while read -r line; do
    120 		openfile "$line" &
    121 	done
    122 fi