OLD | NEW |
(Empty) | |
| 1 #!/bin/sh |
| 2 #--------------------------------------------- |
| 3 # xdg-file-dialog |
| 4 # |
| 5 # Utility script to file selection dialogs |
| 6 # on XDG compliant systems. |
| 7 # |
| 8 # Refer to the usage() function below for usage. |
| 9 # |
| 10 # Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at> |
| 11 # |
| 12 # LICENSE: |
| 13 # |
| 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: |
| 20 # |
| 21 # The above copyright notice and this permission notice shall be included |
| 22 # in all copies or substantial portions of the Software. |
| 23 # |
| 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. |
| 31 # |
| 32 #--------------------------------------------- |
| 33 |
| 34 manualpage() |
| 35 { |
| 36 cat << _MANUALPAGE |
| 37 Name |
| 38 |
| 39 xdg-file-dialog -- command line tool for providing file and directory |
| 40 selection dialogs |
| 41 |
| 42 Synopsis |
| 43 |
| 44 xdg-file-dialog openfilename [--title TITLE] [FILENAME] |
| 45 |
| 46 xdg-file-dialog openfilenamelist [--title TITLE] [FILENAME] |
| 47 |
| 48 xdg-file-dialog savefilename [--title TITLE] [FILENAME] |
| 49 |
| 50 xdg-file-dialog directory [--title TITLE] [DIRNAME] |
| 51 |
| 52 xdg-file-dialog { --help | --manual | --version } |
| 53 |
| 54 Description |
| 55 |
| 56 The xdg-file-dialog program can be used to let the native file selection |
| 57 dialog handle file and directory input. |
| 58 |
| 59 xdg-file-dialog is for use inside a desktop session only. It is not |
| 60 recommended to use xdg-file-dialog as root. |
| 61 |
| 62 Commands |
| 63 |
| 64 openfilename |
| 65 Returns the filename with path for a file to read from. FILENAME |
| 66 can optionally be used to specify path and filename of a |
| 67 preselection. |
| 68 |
| 69 openfilenamelist |
| 70 Returns one or more filenames with path for files to read from, |
| 71 each on a new line. FILENAME can optionally be used to specify |
| 72 path and filename of a preselection. |
| 73 |
| 74 savefilename |
| 75 Returns the filename with path for file to write to. FILENAME can |
| 76 optionally be used to specify path and filename of a preselection. |
| 77 |
| 78 directory |
| 79 Returns the path for an exsiting directory. DIRNAME can optionally |
| 80 be used to specify a path of a preselection. |
| 81 |
| 82 Options |
| 83 |
| 84 --title TITLE |
| 85 Sets the dialog's title (caption) to the specified text. |
| 86 |
| 87 --help |
| 88 Show command synopsis. |
| 89 |
| 90 --manual |
| 91 Show this manualpage. |
| 92 |
| 93 --version |
| 94 Show the xdg-utils version information. |
| 95 |
| 96 Exit Codes |
| 97 |
| 98 An exit code of 0 indicates success while a non-zero exit code indicates |
| 99 failure. The following failure codes can be returned: |
| 100 |
| 101 1 |
| 102 Error in command line syntax. |
| 103 |
| 104 2 |
| 105 One of the files passed on the command line did not exist. |
| 106 |
| 107 3 |
| 108 A required tool could not be found. |
| 109 |
| 110 4 |
| 111 The action failed. |
| 112 |
| 113 Examples |
| 114 |
| 115 xdg-file-dialog savefilename /tmp/foobar.png |
| 116 |
| 117 Asks for a save file name starting in directory /tmp and suggesting |
| 118 foobar.png as the filename |
| 119 |
| 120 xdg-file-dialog directory --title "Select a target folder" /tmp |
| 121 |
| 122 Asks for a directory name starting in directory /tmp using the text |
| 123 "Select a target folder" as the dialog's title/caption. |
| 124 _MANUALPAGE |
| 125 } |
| 126 |
| 127 usage() |
| 128 { |
| 129 cat << _USAGE |
| 130 xdg-file-dialog -- command line tool for providing file and directory |
| 131 selection dialogs |
| 132 |
| 133 Synopsis |
| 134 |
| 135 xdg-file-dialog openfilename [--title TITLE] [FILENAME] |
| 136 |
| 137 xdg-file-dialog openfilenamelist [--title TITLE] [FILENAME] |
| 138 |
| 139 xdg-file-dialog savefilename [--title TITLE] [FILENAME] |
| 140 |
| 141 xdg-file-dialog directory [--title TITLE] [DIRNAME] |
| 142 |
| 143 xdg-file-dialog { --help | --manual | --version } |
| 144 |
| 145 _USAGE |
| 146 } |
| 147 |
| 148 #@xdg-utils-common@ |
| 149 |
| 150 #---------------------------------------------------------------------------- |
| 151 # Common utility functions included in all XDG wrapper scripts |
| 152 #---------------------------------------------------------------------------- |
| 153 |
| 154 #------------------------------------------------------------- |
| 155 # Exit script on successfully completing the desired operation |
| 156 |
| 157 exit_success() |
| 158 { |
| 159 if [ $# -gt 0 ]; then |
| 160 echo "$@" |
| 161 echo |
| 162 fi |
| 163 |
| 164 exit 0 |
| 165 } |
| 166 |
| 167 |
| 168 #----------------------------------------- |
| 169 # Exit script on malformed arguments, not enough arguments |
| 170 # or missing required option. |
| 171 # prints usage information |
| 172 |
| 173 exit_failure_syntax() |
| 174 { |
| 175 if [ $# -gt 0 ]; then |
| 176 echo "xdg-file-dialog: $@" >&2 |
| 177 echo "Try 'xdg-file-dialog --help' for more information." >&2 |
| 178 else |
| 179 usage |
| 180 echo "Use 'man xdg-file-dialog' or 'xdg-file-dialog --manual' for additi
onal info." |
| 181 fi |
| 182 |
| 183 exit 1 |
| 184 } |
| 185 |
| 186 #------------------------------------------------------------- |
| 187 # Exit script on missing file specified on command line |
| 188 |
| 189 exit_failure_file_missing() |
| 190 { |
| 191 if [ $# -gt 0 ]; then |
| 192 echo "xdg-file-dialog: $@" >&2 |
| 193 fi |
| 194 |
| 195 exit 2 |
| 196 } |
| 197 |
| 198 #------------------------------------------------------------- |
| 199 # Exit script on failure to locate necessary tool applications |
| 200 |
| 201 exit_failure_operation_impossible() |
| 202 { |
| 203 if [ $# -gt 0 ]; then |
| 204 echo "xdg-file-dialog: $@" >&2 |
| 205 fi |
| 206 |
| 207 exit 3 |
| 208 } |
| 209 |
| 210 #------------------------------------------------------------- |
| 211 # Exit script on failure returned by a tool application |
| 212 |
| 213 exit_failure_operation_failed() |
| 214 { |
| 215 if [ $# -gt 0 ]; then |
| 216 echo "xdg-file-dialog: $@" >&2 |
| 217 fi |
| 218 |
| 219 exit 4 |
| 220 } |
| 221 |
| 222 |
| 223 #---------------------------------------- |
| 224 # Checks for shared commands, e.g. --help |
| 225 |
| 226 check_common_commands() |
| 227 { |
| 228 while [ $# -gt 0 ] ; do |
| 229 parm=$1 |
| 230 shift |
| 231 |
| 232 case $parm in |
| 233 --help) |
| 234 usage |
| 235 echo "Use 'man xdg-file-dialog' or 'xdg-file-dialog --manual' for ad
ditional info." |
| 236 exit_success |
| 237 ;; |
| 238 |
| 239 --manual) |
| 240 manualpage |
| 241 exit_success |
| 242 ;; |
| 243 |
| 244 --version) |
| 245 echo "xdg-file-dialog 1.0beta1" |
| 246 exit_success |
| 247 ;; |
| 248 esac |
| 249 done |
| 250 } |
| 251 |
| 252 check_common_commands "$@" |
| 253 |
| 254 #-------------------------------------- |
| 255 # Checks for known desktop environments |
| 256 # set variable DE to the desktop environments name, lowercase |
| 257 |
| 258 detectDE() |
| 259 { |
| 260 if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde; |
| 261 elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome; |
| 262 elif xprop -root _DT_SAVE_MODE | grep ' = \"xfce4\"$' >/dev/null 2>&1; then
DE=xfce; |
| 263 fi |
| 264 } |
| 265 |
| 266 #---------------------------------------------------------------------------- |
| 267 |
| 268 |
| 269 |
| 270 open_kde() |
| 271 { |
| 272 DIALOG=`which kdialog` |
| 273 if [ $? -eq 0 ]; then |
| 274 if [ x"$TITLE" != x"" ]; then |
| 275 $DIALOG --title "$TITLE" --getopenfilename "$1" "" |
| 276 else |
| 277 $DIALOG --getopenfilename "$1" "" |
| 278 fi |
| 279 |
| 280 if [ $? -eq 0 ]; then |
| 281 exit_success |
| 282 else |
| 283 exit_failure_operation_failed |
| 284 fi |
| 285 else |
| 286 exit_failure_operation_impossible |
| 287 fi |
| 288 } |
| 289 |
| 290 open_zenity() |
| 291 { |
| 292 DIALOG=`which zenity` |
| 293 if [ $? -eq 0 ]; then |
| 294 if [ x"$1" != x"" ]; then |
| 295 cd `dirname "$1"` 2>/dev/null |
| 296 FILENAME=`basename "$1"` |
| 297 if [ x"$FILENAME" != x"" ]; then |
| 298 FILENAME="--filename=""$FILENAME" |
| 299 fi |
| 300 fi |
| 301 |
| 302 if [ x"$FILENAME" != x"" ]; then |
| 303 if [ x"$TITLE" != x"" ]; then |
| 304 $DIALOG --title "$TITLE" --file-selection "$FILENAME" |
| 305 else |
| 306 $DIALOG --file-selection "$FILENAME" |
| 307 fi |
| 308 else |
| 309 if [ x"$TITLE" != x"" ]; then |
| 310 $DIALOG --title "$TITLE" --file-selection |
| 311 else |
| 312 $DIALOG --file-selection |
| 313 fi |
| 314 fi |
| 315 |
| 316 if [ $? -eq 0 ]; then |
| 317 exit_success |
| 318 else |
| 319 exit_failure_operation_failed |
| 320 fi |
| 321 else |
| 322 exit_failure_operation_impossible |
| 323 fi |
| 324 } |
| 325 |
| 326 open_multi_kde() |
| 327 { |
| 328 DIALOG=`which kdialog` |
| 329 if [ $? -eq 0 ]; then |
| 330 if [ x"$TITLE" != x"" ]; then |
| 331 $DIALOG --title "$TITLE" --multiple --separate-output \ |
| 332 --getopenfilename "$1" "" |
| 333 else |
| 334 $DIALOG --multiple --separate-output --getopenfilename "$1" "" |
| 335 fi |
| 336 |
| 337 if [ $? -eq 0 ]; then |
| 338 exit_success |
| 339 else |
| 340 exit_failure_operation_failed |
| 341 fi |
| 342 else |
| 343 exit_failure_operation_impossible |
| 344 fi |
| 345 } |
| 346 |
| 347 open_multi_zenity() |
| 348 { |
| 349 DIALOG=`which zenity` |
| 350 if [ $? -eq 0 ]; then |
| 351 if [ x"$1" != x"" ]; then |
| 352 cd `dirname "$1"` 2>/dev/null |
| 353 FILENAME=`basename "$1"` |
| 354 if [ x"$FILENAME" != x"" ]; then |
| 355 FILENAME="--filename=""$FILENAME" |
| 356 fi |
| 357 fi |
| 358 |
| 359 if [ x"$FILENAME" != x"" ]; then |
| 360 if [ x"$TITLE" != x"" ]; then |
| 361 LIST=`$DIALOG --title "$TITLE" --multiple --file-selection "$FIL
ENAME"` |
| 362 else |
| 363 LIST=`$DIALOG --multiple --file-selection "$FILENAME"` |
| 364 fi |
| 365 else |
| 366 if [ x"$TITLE" != x"" ]; then |
| 367 LIST=`$DIALOG --title "$TITLE" --multiple --file-selection` |
| 368 else |
| 369 LIST=`$DIALOG --multiple --file-selection` |
| 370 fi |
| 371 fi |
| 372 |
| 373 if [ $? -eq 0 ]; then |
| 374 echo "$LIST" | sed s#'|'#\\n#g |
| 375 exit_success |
| 376 else |
| 377 exit_failure_operation_failed |
| 378 fi |
| 379 else |
| 380 exit_failure_operation_impossible |
| 381 fi |
| 382 } |
| 383 |
| 384 save_kde() |
| 385 { |
| 386 DIALOG=`which kdialog` |
| 387 if [ $? -eq 0 ]; then |
| 388 if [ x"$TITLE" != x"" ]; then |
| 389 $DIALOG --title "$TITLE" --getsavefilename "$1" "" |
| 390 else |
| 391 $DIALOG --getsavefilename "$1" "" |
| 392 fi |
| 393 |
| 394 if [ $? -eq 0 ]; then |
| 395 exit_success |
| 396 else |
| 397 exit_failure_operation_failed |
| 398 fi |
| 399 else |
| 400 exit_failure_operation_impossible |
| 401 fi |
| 402 } |
| 403 |
| 404 save_zenity() |
| 405 { |
| 406 DIALOG=`which zenity` |
| 407 if [ $? -eq 0 ]; then |
| 408 if [ x"$1" != x"" ]; then |
| 409 cd `dirname "$1"` 2>/dev/null |
| 410 FILENAME=`basename "$1"` |
| 411 if [ x"$FILENAME" != x"" ]; then |
| 412 FILENAME="--filename=""$FILENAME" |
| 413 fi |
| 414 fi |
| 415 |
| 416 if [ x"$FILENAME" != x"" ]; then |
| 417 if [ x"$TITLE" != x"" ]; then |
| 418 $DIALOG --title "$TITLE" --save --file-selection "$FILENAME" |
| 419 else |
| 420 $DIALOG --save --file-selection "$FILENAME" |
| 421 fi |
| 422 else |
| 423 if [ x"$TITLE" != x"" ]; then |
| 424 $DIALOG --title "$TITLE" --save --file-selection |
| 425 else |
| 426 $DIALOG --save --file-selection |
| 427 fi |
| 428 fi |
| 429 |
| 430 if [ $? -eq 0 ]; then |
| 431 exit_success |
| 432 else |
| 433 exit_failure_operation_failed |
| 434 fi |
| 435 else |
| 436 exit_failure_operation_impossible |
| 437 fi |
| 438 } |
| 439 |
| 440 directory_kde() |
| 441 { |
| 442 DIALOG=`which kdialog` |
| 443 if [ $? -eq 0 ]; then |
| 444 if [ x"$TITLE" != x"" ]; then |
| 445 $DIALOG --title "$TITLE" --getexistingdirectory "$1" "" |
| 446 else |
| 447 $DIALOG --getexistingdirectory "$1" "" |
| 448 fi |
| 449 |
| 450 if [ $? -eq 0 ]; then |
| 451 exit_success |
| 452 else |
| 453 exit_failure_operation_failed |
| 454 fi |
| 455 else |
| 456 exit_failure_operation_impossible |
| 457 fi |
| 458 } |
| 459 |
| 460 directory_zenity() |
| 461 { |
| 462 DIALOG=`which zenity` |
| 463 if [ $? -eq 0 ]; then |
| 464 if [ x"$1" != x"" ]; then |
| 465 cd "$1" 2>/dev/null |
| 466 fi |
| 467 |
| 468 if [ x"$TITLE" != x"" ]; then |
| 469 $DIALOG --title "$TITLE" --directory --file-selection |
| 470 else |
| 471 $DIALOG --directory --file-selection |
| 472 fi |
| 473 |
| 474 if [ $? -eq 0 ]; then |
| 475 exit_success |
| 476 else |
| 477 exit_failure_operation_failed |
| 478 fi |
| 479 else |
| 480 exit_failure_operation_impossible |
| 481 fi |
| 482 } |
| 483 |
| 484 [ x"$1" != x"" ] || exit_failure_syntax |
| 485 |
| 486 TITLE= |
| 487 action= |
| 488 filename= |
| 489 |
| 490 case $1 in |
| 491 openfilename) |
| 492 action=openfilename |
| 493 ;; |
| 494 |
| 495 openfilenamelist) |
| 496 action=openfilenamelist |
| 497 ;; |
| 498 |
| 499 savefilename) |
| 500 action=savefilename |
| 501 ;; |
| 502 |
| 503 directory) |
| 504 action=directory |
| 505 ;; |
| 506 |
| 507 *) |
| 508 exit_failure_syntax "unknown command '$1'" |
| 509 ;; |
| 510 esac |
| 511 |
| 512 shift |
| 513 |
| 514 while [ $# -gt 0 ] ; do |
| 515 parm=$1 |
| 516 shift |
| 517 |
| 518 case $parm in |
| 519 --title) |
| 520 if [ -z "$1" ] ; then |
| 521 exit_failure_syntax "TITLE argument missing for --title" |
| 522 fi |
| 523 TITLE="$1" |
| 524 shift |
| 525 ;; |
| 526 |
| 527 -*) |
| 528 exit_failure_syntax "unexpected option '$parm'" |
| 529 ;; |
| 530 |
| 531 *) |
| 532 if [ -n "$filename" ] ; then |
| 533 exit_failure_syntax "unexpected argument '$parm'" |
| 534 fi |
| 535 filename="$parm" |
| 536 ;; |
| 537 esac |
| 538 done |
| 539 |
| 540 # Shouldn't happen |
| 541 if [ -z "$action" ] ; then |
| 542 exit_failure_syntax "command argument missing" |
| 543 fi |
| 544 |
| 545 detectDE |
| 546 |
| 547 if [ "$action" = "openfilename" ]; then |
| 548 case "$DE" in |
| 549 kde) |
| 550 open_kde "$filename" |
| 551 ;; |
| 552 |
| 553 gnome|xfce) |
| 554 open_zenity "$filename" |
| 555 ;; |
| 556 |
| 557 *) |
| 558 exit_failure_operation_impossible "no method available for opening a fil
ename dialog" |
| 559 ;; |
| 560 esac |
| 561 elif [ "$action" = "openfilenamelist" ]; then |
| 562 case "$DE" in |
| 563 kde) |
| 564 open_multi_kde "$filename" |
| 565 ;; |
| 566 |
| 567 gnome|xfce) |
| 568 open_multi_zenity "$filename" |
| 569 ;; |
| 570 |
| 571 *) |
| 572 exit_failure_operation_impossible "no method available for opening a fil
ename dialog" |
| 573 ;; |
| 574 esac |
| 575 elif [ "$action" = "savefilename" ]; then |
| 576 case "$DE" in |
| 577 kde) |
| 578 save_kde "$filename" |
| 579 ;; |
| 580 |
| 581 gnome|xfce) |
| 582 save_zenity "$filename" |
| 583 ;; |
| 584 |
| 585 *) |
| 586 exit_failure_operation_impossible "no method available for opening a fil
ename dialog" |
| 587 ;; |
| 588 esac |
| 589 elif [ "$action" = "directory" ]; then |
| 590 case "$DE" in |
| 591 kde) |
| 592 directory_kde "$filename" |
| 593 ;; |
| 594 |
| 595 gnome|xfce) |
| 596 directory_zenity "$filename" |
| 597 ;; |
| 598 |
| 599 *) |
| 600 exit_failure_operation_impossible "no method available for opening a dir
ectory dialog" |
| 601 ;; |
| 602 esac |
| 603 fi |
OLD | NEW |