fileopener.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/bin/bash
  2. # Execution starts here
  3. # Check if running inside a terminal
  4. if [ -t 1 ]; then
  5. is_tty=true
  6. else
  7. is_tty=false
  8. fi
  9. # Try sourcing configuration file
  10. source $HOME/.config/fileopener.config 2>/dev/null || source /etc/fileopener.config 2>/dev/null
  11. # Check if the configuration file has been loaded
  12. if ! declare -p mime_types >/dev/null; then
  13. if $is_tty; then
  14. echo "Configuration file invalid or missing" 1>&2
  15. else
  16. notify-send "Fileopener" "Configuration file invalid or missing"
  17. fi
  18. exit 1
  19. fi
  20. # Throw error if no argument
  21. if [ -z "$1" ]; then
  22. if $is_tty; then
  23. echo "Invalid usage: no file specified" 1>&2
  24. else
  25. notify-send "Fileopener" "Invalid usage: no file specified"
  26. fi
  27. exit 1
  28. fi
  29. # Removing "file://" from the start of the name, if present, to not have to deal with it later
  30. de_filed_1="${1#file://}"
  31. # Get the mime type of the argument
  32. type=$(file --mime-type "$de_filed_1" 2>/dev/null)
  33. type="${type##* }"
  34. type="${type%%/*}"
  35. # If the mime type is empty
  36. if [ -z "$type" ]; then
  37. # if it's not a type, try with a url
  38. # Try to see if any known URL matches, if it does, open the relative program and terminate the opener
  39. for i in "${!url_types[@]}"; do
  40. if echo $de_filed_1 | grep -Eq "^${url_types[$i]}"; then
  41. if $is_tty; then
  42. ${url_openers_terminal[$i]} "$de_filed_1"
  43. else
  44. ${url_openers[$i]} "$de_filed_1"
  45. fi
  46. exit 0
  47. fi
  48. done
  49. # If it's not a URL either, the argument is invalid -> throw an error
  50. if $is_tty; then
  51. echo "Error: $1 is not a file/directory/known URL" 1>&2;
  52. else
  53. notify-send "Fileopener" "\"$1\" is not a file or knwon URL"
  54. fi
  55. exit 1
  56. fi
  57. # Do the same as URLs, but for extensions
  58. for i in ${!extensions[@]}; do
  59. if [ "${de_filed_1##*.}" == "${extensions[$i]}" ]; then
  60. if $is_tty; then
  61. ${extension_openers[$i]} "$de_filed_1"
  62. else
  63. ${extension_openers_terminal[$i]} "$de_filed_1"
  64. fi
  65. exit 0
  66. fi
  67. done
  68. # if the chosen file is an application, run it
  69. if [ "$type" == "application" ]; then
  70. # If the file name doesn't start with either "/" or "./", prepend "./" to it before running
  71. if echo $de_filed_1 | grep -Eq '^(/|\./)'; then
  72. "$de_filed_1"
  73. else
  74. ./"$de_filed_1"
  75. fi
  76. exit 0
  77. fi
  78. # Do the same as URLs, but for mime types
  79. for i in ${!mime_types[@]}; do
  80. if [ "$type" == "${mime_types[$i]}" ]; then
  81. if $is_tty; then
  82. ${mime_openers_terminal[$i]} "$de_filed_1"
  83. else
  84. ${mime_openers[$i]} "$de_filed_1"
  85. fi
  86. exit 0
  87. fi
  88. done
  89. if $is_tty; then
  90. echo "Unknown mime type: $type" 1>&2
  91. else
  92. notify-send "Fileopener" "Unknown type \"$type\""
  93. fi
  94. exit 1