blob: 4beaecc137b2403018c207c6a1c8b9e4439cf62a (
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
|
#!/bin/bash
#
# A command line utility that mimics xdg-open
# Requires Bash 4.0+
#
# Arg check
(( $# )) || { printf "Usage: %s <file>\n" "${0##*/}" >&2; exit 1; }
# Declare an associative array to keep track of our file types.
# Index elements can be a full MIME type (e.g. image/png), just
# the major MIME type (e.g. image) or a file extension (png).
declare -A handler
# To keep things clean, general programs should be declared for
# groups of filetypes resulting in the same program being used
# when a major MIME type won't correctly identify all filetypes.
# openoffice.org documents are an example of this.
doc=soffice
image="sxiv -Z"
video=mplayer
default=${EDITOR:-vi} # Fallback -- should be a text editor
handler[image/svg+xml]=viewnior
handler[inode/directory]=dolphin
handler[application/pdf]=evince
handler[application/vnd.oasis.opendocument.text]=$doc
handler[application/xml]=firefox
handler[doc]=$doc
handler[image]=$image
handler[odb]=$doc
handler[odf]=$doc
handler[ods]=$doc
handler[text/rtf]=$doc
handler[video]=$video
handler[mkv]=$video
handler[xls]=$doc
# Determine the MIME type via 'file' and assign it to an array
# mimetype[0] = major (e.g. image)
# mimetype[1] = minor (e.g. png)
IFS='/' read -r -d ';' -a mimetype < <(file -bi "$1")
# Determine the extension as a fallback method
ext=${1//*.}
# Try to open by exact MIME type
if [[ -n ${handler[${mimetype[0]}/${mimetype[1]}]} ]]; then
${handler[${mimetype[0]}/${mimetype[1]}]} "$@"
# Try to open by major MIME type
elif [[ -n ${handler[${mimetype[0]}]} ]]; then
${handler[${mimetype[0]}]} "$@"
# Try to open by extension
elif [[ -n ${handler[$ext]} ]]; then
${handler[$ext]} "$@"
# Well, I'm out of ideas. Use the $default.
else
$default "$@"
fi
|