-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwhichapp
More file actions
executable file
·89 lines (79 loc) · 2.04 KB
/
Copy pathwhichapp
File metadata and controls
executable file
·89 lines (79 loc) · 2.04 KB
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
#!/usr/bin/env bash
# Print the absolute path of the executable GNOME would use to open the given file.
# On failure, print a diagnostic to stderr and exit 1.
#
# Usage:
#
# whichapp <FILE>
#
# Examples:
#
# whichapp ~/Documents/notes.txt
# whichapp image.png
#
# author: andreasl
set -euo pipefail
show_help() {
script_name="${0##*/}"
msg="${script_name}\n"
msg+="Print the absolute path of the executable GNOME would use to open the given file.\n"
msg+="On failure, print a diagnostic to stderr and exit 1.\n"
msg+="\n"
msg+="Usage:\n"
msg+=" ${script_name} [OPTIONS] <FILE>\n"
msg+="\n"
msg+="Options:\n"
msg+=" -h, --help Print this help message and exit.\n"
msg+="\n"
msg+="Examples:\n"
msg+=" ${script_name} ~/Documents/notes.txt\n"
msg+=" ${script_name} image.png\n"
printf "$msg"
}
case "${1:-}" in
-h | --help)
show_help
exit 0
;;
"")
show_help >&2
exit 1
;;
esac
file="$1"
mime=$(gio info --attributes=standard::content-type "$file" |
awk '/content-type/ {print $NF; exit}')
desktop=$(gio mime "$mime" | awk -F': ' '
/^Default application/ { def = $2 }
/^Registered applications:/ { reg = 1; next }
reg && /^\t/ && !first { gsub(/^\t/, ""); first = $0 }
END { print (def ? def : first) }')
[[ -z $desktop ]] && {
echo "whichapp: no handler for $mime" >&2
exit 1
}
search_dirs=(
"${XDG_DATA_HOME:-$HOME/.local/share}/applications"
/usr/local/share/applications
/usr/share/applications
"$HOME/.local/share/flatpak/exports/share/applications"
/var/lib/flatpak/exports/share/applications
/var/lib/snapd/desktop/applications
)
path=
for dir in "${search_dirs[@]}"; do
[[ -f $dir/$desktop ]] && {
path=$dir/$desktop
break
}
done
[[ -z $path ]] && {
echo "whichapp: $desktop not found" >&2
exit 1
}
cmd=$(grep -m1 '^Exec=' "$path" | cut -d= -f2- |
sed -E 's/%[UFufick]//g' | awk '{print $1}' | tr -d '"')
command -v "$cmd" || {
echo "whichapp: cannot resolve $cmd" >&2
exit 1
}