places 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/bin/sh
  2. # dash_places_menu.sh - a shell (hopefully dash!) places openbox pipe menu
  3. # Copyright (C) 2010 John Crawley
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. # Version 2012/02/17
  15. # Usage: add
  16. # <menu id="places" label="Places" execute="/path/to/dash_places_menu.sh ~/" />
  17. # to your .config/openbox/menu.xml
  18. # or, if you want the "recent files" menu incorporated at the top, use:
  19. # <menu id="places" label="Places" execute="/path/to/dash_places_menu.sh --recent ~" />
  20. # make sure you have recently_opened_menu.sh somewhere, and enter its path below.
  21. # path to your "recent files" script, if you want to incorporate it:
  22. #recent_script="$HOME"/scripts/recently_opened_menu.sh
  23. # Command to open folders at "Browse here..." - any file manager
  24. open_folder_cmd=spacefm
  25. # Default command to open files with - others might be xdg-open, gnome-open, pcmanfm...
  26. default_open_cmd=spacefm # exo-open comes with thunar
  27. # Text editor of choice
  28. text_editor=leafpad
  29. # function to open files with default open command, or alternative command for certain files
  30. # - add other conditions to choice
  31. open_file() {
  32. [ -x "$1" ] && exec "$text_editor" "$1" # comment out this line if you don't want to edit executables instead of executing
  33. #[ -x "$1" ] && exec "terminator -e" "$1" # uncomment this and comment out previous line to run executables in terminal instead of editing
  34. [ "${1##*.}" = desktop ] && exec "$text_editor" "$1" # comment out this line if you don't want to edit .desktop files instead of executing
  35. exec "$default_open_cmd" "$1" # use default open command if above conditions not satisfied
  36. }
  37. # extra dotfiles to display in HOME folder (dotfiles are hidden by default)
  38. # edit the list (space separated, surrounded by single quotes) or comment this line out, to taste:
  39. shown_dotfiles='.config .local .Xdefaults .bash_aliases .bashrc .fonts.conf .gtkrc-2.0.mine .profile .xsession-errors'
  40. # By default, this script will display directories separately, before files.
  41. # To change this behaviour, see NOTE1, NOTE2 and NOTE3 below, near end of page.
  42. #######################################################################
  43. case $1 in
  44. # if "--open" option is sent as $1, open file ($2) instead of generating menu
  45. --open)
  46. open_file "$2"
  47. echo "$0 : failed to open $2" >&2
  48. exit;; # in case exec command fails
  49. # if "--recent" option is sent, incorporate "recent files" menu
  50. --recent)
  51. shift
  52. output='<openbox_pipe_menu>
  53. '
  54. if [ -x "$recent_script" ]
  55. then
  56. output="$output"'<separator label="Recently opened..." />
  57. <menu execute="'"$recent_script"'" id="recent" label="files" />
  58. '
  59. else
  60. echo "$0 : cannot find executable script $recent_script" >&2
  61. fi;;
  62. *)
  63. output='<openbox_pipe_menu>
  64. ';;
  65. esac
  66. path="${1:-$HOME}" # default starting place is ~, otherwise $1
  67. path="$( echo "${path}"/ | tr -s '/' )" # ensure one final slash
  68. [ -d "$path" ] || { echo "$0 : $path is not a directory" >&2; exit 1; }
  69. case "$path" in # only escape if string needs it
  70. *\&*|*\<*|*\>*|*\"*|*\'*) pathe=$(sed "s/\&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g;s/\"/\&quot;/g;s/'/\&apos;/g;") <<XXX
  71. $path
  72. XXX
  73. ;;
  74. *)pathe=$path;;
  75. esac
  76. case "$pathe" in
  77. *\&apos\;*) pathe_apos=$(sed 's/\&apos;/\&apos;\&quot;\&apos;\&quot;\&apos;/g;')<<XXX
  78. $pathe
  79. XXX
  80. ;;
  81. *) pathe_apos=$pathe;;
  82. esac
  83. output="$output"'<separator label="'$pathe'" />
  84. <item label="Browse here...">
  85. <action name="Execute">
  86. <command>
  87. &apos;'"$open_folder_cmd"'&apos; &apos;'"$pathe_apos"'&apos;
  88. </command>
  89. </action>
  90. </item>
  91. <separator />
  92. '
  93. unset extra_entries directories_menu files_menu
  94. [ "$path" = "$HOME"/ ] && extra_entries="$shown_dotfiles"
  95. for i in "$path"* $extra_entries
  96. do
  97. [ -e "$i" ] || continue # only output code if file exists
  98. shortname="${i##*/}"
  99. case $shortname in
  100. *\&*|*\<*|*\>*|*\"*|*\'*) shortnamee=$(sed "s/\&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g;s/\"/\&quot;/g;s/'/\&apos;/g;") <<XXX
  101. $shortname
  102. XXX
  103. ;;
  104. *) shortnamee=$shortname;;
  105. esac
  106. case $shortnamee in
  107. *\&apos\;*) shortnamee_apos=$(sed 's/\&apos;/\&apos;\&quot;\&apos;\&quot;\&apos;/g;')<<XXX
  108. $shortnamee
  109. XXX
  110. ;;
  111. *) shortnamee_apos=$shortnamee;;
  112. esac
  113. [ -d "$i" ] && {
  114. # NOTE1 If you want directories and files listed together
  115. # change next line (directories_menu="$directories_menu"') to read: files_menu="$files_menu"' (note the one single quote at the end)
  116. directories_menu="$directories_menu"'
  117. <menu id="'"${pathe_apos}${shortnamee_apos}"'" label="'"$shortnamee"'" execute="&apos;'"$0"'&apos; &apos;'"${pathe_apos}${shortnamee_apos}"'&apos;" />'; continue; }
  118. files_menu="$files_menu"'
  119. <item label="'"$shortnamee"'">
  120. <action name="Execute">
  121. <command>
  122. &apos;'"$0"'&apos; --open &apos;'"${pathe_apos}${shortnamee_apos}"'&apos;
  123. </command>
  124. </action>
  125. </item>'
  126. done
  127. [ -n "$directories_menu" ] && {
  128. # NOTE2 comment out next 2 lines if you don't want "Directories" label
  129. output="${output}"'<separator label="Directories" />
  130. '
  131. output="${output}${directories_menu}"'
  132. '; }
  133. [ -n "$files_menu" ] && {
  134. # NOTE3 comment out next 2 lines if you don't want "Files" label
  135. output="${output}"'<separator label="Files" />
  136. '
  137. output="${output}${files_menu}"'
  138. '; }
  139. output="${output}"'</openbox_pipe_menu>
  140. '
  141. printf '%s' "$output"
  142. exit