-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen
executable file
·213 lines (174 loc) · 4.31 KB
/
open
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
205
206
207
208
209
210
#!/usr/bin/env zsh
#
# open
# Provide OS X's open utility on GNU/Linux
#
SCRIPT_NAME="open"
SCRIPT_VERSION="0.2"
source $(dirname $0)/_base.inc.sh
##########################################
# Functions
#
#
# Show some information about this script
#
version() {
echo "$SCRIPT_NAME $SCRIPT_VERSION"
echo "Provides macOS's open utility on GNU/Linux"
}
#
# Display a usage help
#
usage() {
echo "Usage:"
echo " $SCRIPT_NAME [OPTIONS] [FILENAME]"
echo
echo "Options:"
echo " --version -V Show the version and exit"
echo " --help -h Show this help and exit"
echo " -a <App> Open the file using <App>"
echo " -t Edit the file in \$VISUAL"
echo " -e Edit the file in \$EDITOR (implies --no-fork)"
echo " --fork Start a GUI application without blocking the terminal"
echo " (fork to background)"
echo " --no-fork Start application in foreground"
echo
echo "For \"-a <App>\": If the filename is omitted, the application is"
echo "started without arguments."
echo "The original open command uses TextEdit.app with \"-t\", and"
echo "your default text editor with \"-e\""
echo
}
#
# Helper used to launch "gio open"
# Since $APP cannot contain arguments/parameters, we have to use this workaround.
#
gio_open() {
if ! which gio &>/dev/null
then
return 1
fi
gio open $@
}
#
# Detect the system's default application for opening files.
#
get_default_app() {
if [[ $OS == "Mac OS X" ]]; then
APP="/usr/bin/open"
elif which gio &>/dev/null; then
APP="gio_open"
elif which xdg-open &>/dev/null; then
APP="xdg-open"
else
echo "$SCRIPT_NAME: Error: Neither gio or xdg-open found in \$PATH!" >&2
exit 1
fi
echo "$APP"
}
##########################################
# Parse command line
#
if [[ $# -eq 0 ]]; then
usage >&2
echo exit 1
fi
if [[ -z "$DISPLAY" && $OS != "Mac OS X" ]]; then
#echo "$SCRIPT_NAME: Error: \$DISPLAY not set!" >&2
message_error "\$DISPLAY not set! ${_ANSI_ATTR_ITALIC}open${_ANSI_ATTR_ITALIC_RESET} only works on X11" >&2
exit 1
fi
APP=$(get_default_app)
OPT_HELP=0
OPT_FORK=1
FILES=()
DEBUG=${DEBUG:-0}
while [[ $# -gt 0 ]]; do
if [[ "$1" == "--version" || "$1" == "-V" ]]; then
version
exit 0
elif [[ "$1" == "--help" || "$1" == "-h" ]]; then
version
usage
exit 0
elif [[ "$1" == "--debug" ]]; then
DEBUG=1
elif [[ "$1" == "--fork" ]]; then
OPT_FORK=1
elif [[ "$1" == "--no-fork" ]]; then
OPT_FORK=0
elif [[ "$1" == "-a" ]]; then
if [[ $# -lt 2 || ${2:0:1} == "-" ]]; then
usage >&2
exit 1
fi
shift
APP="$1"
elif [[ "$1" == "-t" ]]; then
if [[ -z "$VISUAL" ]]; then
message_error "You need to set \$VISUAL to use \"-t\"."
exit 1
fi
APP=$VISUAL
elif [[ "$1" == "-e" ]]; then
if [[ -z "$EDITOR" ]]; then
message_error "You need to set \$EDITOR to use \"-e\"."
exit 1
fi
APP=$EDITOR
OPT_FORK=0
elif [[ ${1:0:1} == "-" ]]; then
usage >&2
exit 1
else
FILES+="$1"
fi
shift
done
FILES_COUNT=${#FILES[@]}
_debug_files=""
for f in ${FILES[@]}; do
_debug_files+="\"$f\""
done
message_debug "APP = \"$APP\""
message_debug "FILES = ($_debug_files)"
message_debug "\${#FILES[@]} = ${#FILES[@]}"
unset _debug_files
#[[ $DEBUG -eq 1 ]] && exit 0
##########################################
# Launch app if no files given
#
if [[ ${#FILES[@]} -eq 0 ]]; then
message_debug "No files given. Launching \$APP..."
exit 42
fi
##########################################
# Open all files/URLs from $FILES
#
for FILE in ${FILES[@]}; do
# Check if file is readable or looks like a valid URL
if [[ $FILE == *:/* || ! -r $FILE ]]; then
echo "$SCRIPT_NAME: File not found or not readable: $FILE" >&2
exit 1
fi
# Ensure a valid value for $APP
if ! which $APP &>/dev/null; then
echo "$SCRIPT_NAME: Command not found: $APP" >&2
exit 1
fi
if [[ $FILE_COUNT -eq 1 ]]; then
fi
# Open file using $APP, detached from the terminal
$APP "$FILE" </dev/null &>/dev/null &|
# Open file using $APP
if [[ $OPT_FORK -eq 1 ]]; then
# Run detached from the terminal (fork to background)
message_debug "Running '$APP \"$(realpath ${FILE})\"' (detached from TTY)"
$APP "${FILE}" </dev/null &>/dev/null &|
else
# Run in foreground
message_debug "Running '$APP \"$(realpath ${FILE})\"'"
#$APP "$(realpath ${FILE})"
$APP "${FILE}"
fi
done