aboutsummaryrefslogtreecommitdiff
path: root/open-file
blob: 024f81a4c2d0f61a1d6e5cc8c0cd23ffa40f52e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/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" && continue
	[ -z "$mimetype" ] && mimetype="$(file --mime-type "$f" -bL)"
	case "$mimetype" in
		video/*)
			$launcher $video "$f"
			;;
		audio/*)
			$launcher $music "$f"
			;;
		application/pdf | application/postscript | image/vnd.djvu)
			$launcher $pdf "$f"
			;;
		image/*)
			$launcher $img "$f"
			;;
		application/msword | \
		application/vnd.oasis.opendocument.text | text/rtf | \
		application/vnd.openxmlformats-officedocument.wordprocessingml.*)
			$launcher $word "$f"
			;;
		application/ms-excel | application/vnd.oasis.opendocument.spreadsheet | \
		text/rtf | application/vnd.openxmlformats-officedocument.spreadsheetml.*)
			$launcher $sheet "$f"
			;;
		text/html | text/enriched)
			$launcher $html "$f"
			;;
		text/*)
			$launcher $xedit "$f"
			;;
		application/zip)
			unzip "$f"
			;;
		application/x-rar-compressed)
			unrar "$f"
			;;
		application/x-archive | application/x-tar | \
		application/x-bzip2 | application/gzip | application/x-lzip | \
		application/x-lzma | application/x-xz | application/x-gtar)
			tar -tavf "$f"
			;;
		application/java-archive)
			java -jar "$f"
			;;
		*)
			prog=$(dmenu_path | $menu -p "How to open $f ?")
			if [ -n "$prog" ]; then
				$launcher $prog "$f"
				else
				exit 1
			fi
			;;
	esac
done

Generated with cgit - Back to sebastiano.tronto.net