-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathopen-file-explorer.sh
executable file
·204 lines (146 loc) · 5.89 KB
/
open-file-explorer.sh
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/bash
# Version 1.03.
#
# This script opens a file explorer on the given file or directory.
#
# Which file explorer is started depends on the underlying operating system,
# but you can hard-code your choice below.
#
# I wrote this script because I often want to copy files from the current directory with the mouse,
# or I just want a standard OS file explorer window that shows the file I am currently editing in Emacs.
# Each operating system and desktop environment is different, so this script abstracts all differences
# and opens a file explorer that shows the given file or directory, with as much comfort as the
# underlying platform allows.
#
# Copyright (c) 2019-2022 R. Diez - Licensed under the GNU AGPLv3
set -o errexit
set -o nounset
set -o pipefail
declare -r SCRIPT_NAME="${BASH_SOURCE[0]##*/}" # This script's filename only, without any path components.
declare -r -i BOOLEAN_TRUE=0
declare -r -i BOOLEAN_FALSE=1
declare -r -i EXIT_CODE_ERROR=1
abort ()
{
echo >&2 && echo "Error in script \"$SCRIPT_NAME\": $*" >&2
exit $EXIT_CODE_ERROR
}
str_ends_with ()
{
# $1 = string
# $2 = suffix
case "$1" in
*$2) return $BOOLEAN_TRUE;;
*) return $BOOLEAN_FALSE;;
esac
}
is_tool_installed ()
{
if command -v "$1" >/dev/null 2>&1 ;
then
return $BOOLEAN_TRUE
else
return $BOOLEAN_FALSE
fi
}
verify_start_detached_is_installed ()
{
if is_tool_installed "$SDNAME"; then
return
fi
abort "This script needs script $SDNAME . Please make sure $SDNAME is on the PATH."
}
# ------- Entry point -------
declare -r ARG_ERR_MSG="This script expects exactly one argument with the filename or directory name, optionally after an '--' separator."
case $# in
1) if [[ $1 = "--" ]]; then
abort "$ARG_ERR_MSG"
fi;;
2) if [[ $1 = "--" ]]; then
shift
else
abort "$ARG_ERR_MSG"
fi;;
*) abort "$ARG_ERR_MSG";;
esac
declare -r FILE_OR_DIR_NAME="$1"
# Check that the file or directory does exist.
#
# It is not clear whether all file managers display an error message if it does not.
# For example, in the case of 'Caja' (from the MATE Desktop), we cannot pass a filename,
# and the base directory may exist. If we do not check for existence, then the user
# may not realise (or realise too late) that the requested file does not exist.
# I do not think that such a behaviour is a good idea, so always check upfront.
if [ ! -e "$FILE_OR_DIR_NAME" ]; then
abort "File or directory \"$FILE_OR_DIR_NAME\" does not exist."
fi
# You normally need to start the file manager process in the background (with StartDetached.sh or similar).
# Otherwise, if this is the first file manager window, most file managers do not terminate until
# the last window is gone, so the current console or process forever blocks.
declare -r SDNAME="StartDetached.sh"
if false; then
# Here you can manually edit the code below and use the command you want.
# But is normally best to use the automatic desktop environment detection logic below.
if false; then
verify_start_detached_is_installed
printf -v CMD "%q nautilus --no-desktop --browser -- %q" "$SDNAME" "$FILE_OR_DIR_NAME"
else
# This uses the standard "xdg-open" tool.
if [ -d "$FILE_OR_DIR_NAME" ]; then
NAME_TO_OPEN="$FILE_OR_DIR_NAME"
else
# Open the containing directory.
NAME_TO_OPEN="$(dirname -- "$FILE_OR_DIR_NAME")"
fi
printf -v CMD "xdg-open %q" "$NAME_TO_OPEN"
fi
else
verify_start_detached_is_installed
case "${XDG_CURRENT_DESKTOP:-}" in
KDE) # Dolphin's --select option behaves differently when a directory name ends with a slash:
# - With a trailing '/', it opens the given directory.
# This is the behaviour we have chosen here to implement.
# - Without a trailing '/, it opens the parent directory and selects the given directory.
# That applies tot he current directory too, so "." and "./" behave differenty.
if [ -d "$FILE_OR_DIR_NAME" ]; then
# This includes the special case of the root directory '/'.
if str_ends_with "$FILE_OR_DIR_NAME" "/"; then
NAME_TO_OPEN="$FILE_OR_DIR_NAME"
else
NAME_TO_OPEN="${FILE_OR_DIR_NAME}/"
fi
else
NAME_TO_OPEN="$FILE_OR_DIR_NAME"
fi
printf -v CMD "%q dolphin --select %q" "$SDNAME" "$NAME_TO_OPEN"
;;
XFCE) # As of Thunar version 1.8.14 (Xfce 4.14), you cannot pass a filename, because it
# will then open it with the standard tool associated to that file type (or the standard text editor).
if [ -d "$FILE_OR_DIR_NAME" ]; then
NAME_TO_OPEN="$FILE_OR_DIR_NAME"
else
# Thunar has no equivalent to "--select", so just open the containing directory.
NAME_TO_OPEN="$(dirname -- "$FILE_OR_DIR_NAME")"
fi
printf -v CMD "%q thunar %q" "$SDNAME" "$NAME_TO_OPEN"
;;
MATE)
# MATE caja 1.24.0 displays an error if the filename passed is a normal file,
# so we always to have to pass a directory.
if [ -d "$FILE_OR_DIR_NAME" ]; then
NAME_TO_OPEN="$FILE_OR_DIR_NAME"
else
# Caja has no equivalent to "--select", so just open the containing directory.
NAME_TO_OPEN="$(dirname -- "$FILE_OR_DIR_NAME")"
fi
# Option --no-desktop is buggy in Caja version 1.20.2, the default file manager in Ubuntu MATE 18.04.
# See the following bug report:
# https://github.com/mate-desktop/caja/issues/555
# Unsetting environment variable DESKTOP_AUTOSTART_ID seems to work around the issue.
printf -v CMD "%q env --unset=DESKTOP_AUTOSTART_ID caja --browser --no-desktop -- %q" "$SDNAME" "$NAME_TO_OPEN"
;;
*) abort "Could not determine the current desktop environment, please check environment variable XDG_CURRENT_DESKTOP.";;
esac
fi
echo "$CMD"
eval "$CMD"