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