#!/bin/sh # Inspired by https://github.com/salman-abedin/launch.sh # Launches files based on their mimetypes # Use option -s devour to swallow or -s " " to open normally from terminal # Use -t to specify mimetype # Requires: dmenu_path or similar (fallback) # dmenu-filepicker menu=${MENU:-dmenu} video=${VIDEOPLAYER:-mpv} music=${MUSICPLAYER:-mpv} pdf=${VIEWER:-zathura} img=${IMAGEVIEWER:-imv} word=${TEXTDOCUMENT:-libreoffice} sheet=${SPREADSHEET:-libreoffice} html=${HTMLVIEWER:-firefox} xedit=${XEDITOR:-gedit} while getopts "s:t:" opt; do case "$opt" in s) launcher="$OPTARG" ;; t) mimetype="$OPTARG" ;; esac done # If launcher = "", assume running in terminal, so some programs change if [ -z "$launcher" ]; then xedit=${EDITOR:-vi} fi shift `expr $OPTIND - 1` toopen=$@ [ -z "$toopen" ] && toopen=$(dmenu-filepicker) for f in $toopen; do [ ! -f "$f" ] && echo "$f: bad argument" && exit 1 [ -z "$mimetype" ] && mimetype="$(file --mime-type "$f" -bL)" prog="" case "$mimetype" in video/*) prog="$video" ;; audio/*) prog="$music" ;; application/pdf | application/postscript | image/vnd.djvu) prog="$pdf" ;; image/*) prog="$img" ;; application/msword | \ application/vnd.oasis.opendocument.text | text/rtf | \ application/vnd.openxmlformats-officedocument.wordprocessingml.*) prog="$word" ;; application/ms-excel | application/vnd.oasis.opendocument.spreadsheet | \ text/rtf | application/vnd.openxmlformats-officedocument.spreadsheetml.*) prog="$sheet" ;; text/html | text/enriched) prog="$html" ;; text/*) prog="$xedit" ;; application/zip) prog="unzip" ;; application/x-rar-compressed) prog="unrar" ;; application/x-archive | application/x-tar | \ application/x-bzip2 | application/gzip | application/x-lzip | \ application/x-lzma | application/x-xz | application/x-gtar) prog="tar -tavf" ;; application/java-archive) prog="java -jar" ;; *) prog=$(dmenu_path | $menu -p "How to open $f ?") ;; esac if [ -n "$prog" ]; then $launcher $prog "$f" & else exit 1 fi done