2
#---------------------------------------------
5
# Utility script to open a URL in the registered default application.
7
# Refer to the usage() function below for usage.
9
# Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
10
# Copyright 2006, Jeremy White <jwhite@codeweavers.com>
14
# Permission is hereby granted, free of charge, to any person obtaining a
15
# copy of this software and associated documentation files (the "Software"),
16
# to deal in the Software without restriction, including without limitation
17
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
18
# and/or sell copies of the Software, and to permit persons to whom the
19
# Software is furnished to do so, subject to the following conditions:
21
# The above copyright notice and this permission notice shall be included
22
# in all copies or substantial portions of the Software.
24
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30
# OTHER DEALINGS IN THE SOFTWARE.
32
#---------------------------------------------
39
xdg-open - opens a file or URL in the user's preferred application
43
xdg-open { file | URL }
45
xdg-open { --help | --manual | --version }
49
xdg-open opens a file or URL in the user's preferred application. If a URL is
50
provided the URL will be opened in the user's preferred web browser. If a file
51
is provided the file will be opened in the preferred application for files of
52
that type. xdg-open supports file, ftp, http and https URLs.
54
xdg-open is for use inside a desktop session only. It is not recommended to use
60
Show command synopsis.
64
Show the xdg-utils version information.
68
An exit code of 0 indicates success while a non-zero exit code indicates
69
failure. The following failure codes can be returned:
72
Error in command line syntax.
74
One of the files passed on the command line did not exist.
76
A required tool could not be found.
82
xdg-open 'http://www.freedesktop.org/'
84
Opens the Freedesktop.org website in the user's default browser
86
xdg-open /tmp/foobar.png
88
Opens the PNG image file /tmp/foobar.png in the user's default image viewing
97
xdg-open - opens a file or URL in the user's preferred application
101
xdg-open { file | URL }
103
xdg-open { --help | --manual | --version }
110
#----------------------------------------------------------------------------
111
# Common utility functions included in all XDG wrapper scripts
112
#----------------------------------------------------------------------------
116
[ ${XDG_UTILS_DEBUG_LEVEL-0} -lt $1 ] && return 0;
121
#-------------------------------------------------------------
122
# Exit script on successfully completing the desired operation
126
if [ $# -gt 0 ]; then
135
#-----------------------------------------
136
# Exit script on malformed arguments, not enough arguments
137
# or missing required option.
138
# prints usage information
140
exit_failure_syntax()
142
if [ $# -gt 0 ]; then
143
echo "xdg-open: $@" >&2
144
echo "Try 'xdg-open --help' for more information." >&2
147
echo "Use 'man xdg-open' or 'xdg-open --manual' for additional info."
153
#-------------------------------------------------------------
154
# Exit script on missing file specified on command line
156
exit_failure_file_missing()
158
if [ $# -gt 0 ]; then
159
echo "xdg-open: $@" >&2
165
#-------------------------------------------------------------
166
# Exit script on failure to locate necessary tool applications
168
exit_failure_operation_impossible()
170
if [ $# -gt 0 ]; then
171
echo "xdg-open: $@" >&2
177
#-------------------------------------------------------------
178
# Exit script on failure returned by a tool application
180
exit_failure_operation_failed()
182
if [ $# -gt 0 ]; then
183
echo "xdg-open: $@" >&2
189
#------------------------------------------------------------
190
# Exit script on insufficient permission to read a specified file
192
exit_failure_file_permission_read()
194
if [ $# -gt 0 ]; then
195
echo "xdg-open: $@" >&2
201
#------------------------------------------------------------
202
# Exit script on insufficient permission to read a specified file
204
exit_failure_file_permission_write()
206
if [ $# -gt 0 ]; then
207
echo "xdg-open: $@" >&2
215
if [ ! -e "$1" ]; then
216
exit_failure_file_missing "file '$1' does not exist"
218
if [ ! -r "$1" ]; then
219
exit_failure_file_permission_read "no permission to read file '$1'"
223
check_vendor_prefix()
232
echo "xdg-open: filename '$file' does not have a proper vendor prefix" >&2
233
echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
234
echo 'with a dash ("-"). An example filename is '"'example-$file'" >&2
235
echo "Use --novendor to override or 'xdg-open --manual' for additional info." >&2
241
# if the file exists, check if it is writeable
242
# if it does not exists, check if we are allowed to write on the directory
244
if [ ! -w "$1" ]; then
245
exit_failure_file_permission_write "no permission to write to file '$1'"
249
if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
250
exit_failure_file_permission_write "no permission to create file '$1'"
255
#----------------------------------------
256
# Checks for shared commands, e.g. --help
258
check_common_commands()
260
while [ $# -gt 0 ] ; do
267
echo "Use 'man xdg-open' or 'xdg-open --manual' for additional info."
277
echo "xdg-open 1.0beta3"
284
check_common_commands "$@"
285
if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
287
xdg_redirect_output=" > /dev/null 2> /dev/null"
289
# All output to stderr
290
xdg_redirect_output=" >&2"
293
#--------------------------------------
294
# Checks for known desktop environments
295
# set variable DE to the desktop environments name, lowercase
299
if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
300
elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
301
elif xprop -root _DT_SAVE_MODE | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
305
#----------------------------------------------------------------------------
306
# kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
307
# It also always returns 1 in KDE 3.4 and earlier
308
# Simply return 0 in such case
310
kfmclient_fix_exit_code()
312
version=`kde-config --version 2>/dev/null | grep KDE`
313
major=`echo $version | sed 's/KDE: \([0-9]\).*/\1/'`
314
minor=`echo $version | sed 's/KDE: [0-9]*\.\([0-9]\).*/\1/'`
315
release=`echo $version | sed 's/KDE: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
316
test "$major" -gt 3 && return $1
317
test "$minor" -gt 5 && return $1
318
test "$release" -gt 4 && return $1
325
kfmclient_fix_exit_code $?
327
if [ $? -eq 0 ]; then
330
exit_failure_operation_failed
338
if [ $? -eq 0 ]; then
341
exit_failure_operation_failed
349
if [ $? -eq 0 ]; then
352
exit_failure_operation_failed
359
for browser in $BROWSER; do
360
if [ x"$browser" != x"" ]; then
362
browser_with_arg=`echo "$browser" | sed s#%s#"$1"#`
364
if [ x"$browser_with_arg" = x"$browser" ]; then "$browser" "$1";
365
else $browser_with_arg;
368
if [ $? -eq 0 ]; then exit_success;
373
exit_failure_operation_impossible "no method available for opening '$1'"
376
[ x"$1" != x"" ] || exit_failure_syntax
379
while [ $# -gt 0 ] ; do
385
exit_failure_syntax "unexpected option '$parm'"
389
if [ -n "$url" ] ; then
390
exit_failure_syntax "unexpected argument '$parm'"
397
if [ -z "${url}" ] ; then
398
exit_failure_syntax "file or URL argument missing"
403
if [ x"$DE" = x"" ]; then
404
# if BROWSER variable is not set, check some well known browsers instead
405
if [ x"$BROWSER" = x"" ]; then
406
BROWSER=firefox:mozilla:netscape
425
exit_failure_operation_impossible "no method available for opening '$url'"