summaryrefslogtreecommitdiff
path: root/scripts/open-file
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2026-05-06 08:30:49 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2026-05-06 08:30:49 +0200
commit60052659b98832c78c902e4f27ecee2663d8e913 (patch)
tree9efc1388569c43456557aa1975b4bec99bf06c94 /scripts/open-file
parent10e16c2fb2266bb2b46ec7ea64d721de0c29a0d9 (diff)
downloadconfig-60052659b98832c78c902e4f27ecee2663d8e913.tar.gz
config-60052659b98832c78c902e4f27ecee2663d8e913.zip
Added scripts
Diffstat (limited to 'scripts/open-file')
-rwxr-xr-xscripts/open-file122
1 files changed, 122 insertions, 0 deletions
diff --git a/scripts/open-file b/scripts/open-file
new file mode 100755
index 0000000..c985bf4
--- /dev/null
+++ b/scripts/open-file
@@ -0,0 +1,122 @@
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
9menu="dmenu -i -l 15"
10
11# Change default apps here
12video=${VIDEO:-vlc}
13music=${MUSIC:-vlc}
14pdf=${PDF:-zathura}
15img="${IMGVIEW:-nsxiv}"
16word="${WORDS:-libreoffice}"
17sheet="${SHEETS:-libreoffice}"
18html="${BROWSER:-firefox} --new-window"
19xedit="${XEDIT:-xedit}"
20
21usage() {
22 echo "Usage: open-file [-m MENU] [-s LAUNCHER] [-t MIMETYPE] [files...]"
23}
24
25openfile() {
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
93while 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
109done
110
111shift $((OPTIND - 1))
112
113if [ -n "$1" ]; then
114 while [ -n "$1" ]; do
115 openfile "$1" &
116 shift
117 done
118else
119 dmenu-filepicker -m "$menu" | while read -r line; do
120 openfile "$line" &
121 done
122fi

Generated with cgit - Back to sebastiano.tronto.net