Posted by johnraff on Fri 19th Nov 05:09 (modification of post by johnraff view diff)
View followups from johnraff and aung taejjon | diff | download | new post
- #!/bin/sh
- # shell (hopefully dash!) places menu for openbox
- # Usage: add
- # <menu id="places" label="Places" execute="/path/to/dash_places_menu.sh ~" />
- # to your .config/openbox/menu.xml (you don't need a final slash after the ~)
- # Command to open folders at "Browse here..." - any file manager
- open_folder_cmd=thunar
- # Default command to open files with - others might be xdg-open, gnome-open, pcmanfm...
- default_open_cmd=exo-open # exo-open comes with thunar
- # Text editor of choice
- text_editor=gedit
- # function to open files with default open command, or alternative command for certain files
- # - add other conditions to choice
- open_file() {
- [ -x "$1" ] && exec "$text_editor" "$1" # comment out this line if you don't want to edit executables instead of executing
- #[ -x "$1" ] && exec "terminator -e" "$1" # uncomment this and comment out previous line to run executables in terminal instead of editing
- [ "${1##*.}" = desktop ] && exec "$text_editor" "$1" # comment out this line if you don't want to edit .desktop files instead of executing
- exec "$default_open_cmd" "$1" # use default open command if above conditions not satisfied
- }
- # extra dotfiles to display in HOME folder (dotfiles are hidden by default)
- # edit the list (space separated) or comment this line out, to taste:
- shown_dotfiles='.config .local .Xdefaults .bash_aliases .bashrc .fonts.conf .gtkrc-2.0.mine .profile .xsession-errors'
- # By default, this script will display directories separately, before files.
- # To change this behaviour, see lines 85-87, 100-102 and 106-108.
- #######################################################################
- # if "--open" argument is sent as $1, open file ($2) instead of generating menu
- [ "$1" = '--open' ] && {
- open_file "$2"
- exit # in case exec command fails
- }
- path="${1:-$HOME}" # default starting place is ~, otherwise $1
- path="$( echo "${path}"/ | tr -s '/' )" # ensure one final slash
- [ -d "$path" ] || { echo "$path is not a directory" >&2; exit 1; }
- # escape xml special characters
- escape() {
- case "$1" in # only escape if string needs it
- *\&*|*\<*|*\>*|*\"*|*\'*) sed "s/\&/\&/g;s/</\</g;s/>/\>/g;s/\"/\"/g;s/'/\'/g;" <<XXX
- $1
- XXX
- ;;
- *) printf '%s' "$1";;
- esac
- }
- # escape ' (originally ') with surrounding '""' for command line
- escapos() {
- case "$1" in
- *\&apos\;*) sed 's/\'/\'\"\'\"\'/g;'<<XXX
- $1
- XXX
- ;;
- *) printf '%s' "$1";;
- esac
- }
- pathe="$( escape "$path" )"
- output='<openbox_pipe_menu>
- <separator label="'$pathe'" />
- <item label="Browse here...">
- <action name="Execute">
- <execute>
- '$open_folder_cmd' "'$(escapos "$pathe" )'"
- </execute>
- </action>
- </item>
- <separator />
- '
- unset extra_entries directories_menu files_menu
- [ "$path" = "$HOME"/ ] && extra_entries="$shown_dotfiles"
- for i in "$path"* $extra_entries
- do
- [ -e "$i" ] || continue # only output code if file exists
- shortname="$(escape "${i##*/}")"
- ie="${pathe}${shortname}"
- [ -d "$i" ] && {
- # (note the one single quote at the end) If you want directories and files listed together
- # change next line (directories_menu=$directories_menu') to read: files_menu=$files_menu'
- directories_menu="$directories_menu"'
- <menu id="'"$ie"'" label="'"$shortname"'" execute="''"$0"'' ''"$(escapos "$ie")"''" />'; continue; }
- files_menu="$files_menu"'
- <item label="'"$shortname"'">
- <action name="Execute">
- <execute>
- ''"$0"'' --open ''"$(escapos "$ie")"''
- </execute>
- </action>
- </item>'
- done
- [ -n "$directories_menu" ] && {
- # comment out next 2 lines if you don't want "Directories" label
- output="${output}"'<separator label="Directories" />
- '
- output="${output}${directories_menu}"'
- '; }
- [ -n "$files_menu" ] && {
- # comment out next 2 lines if you don't want "Files" label
- output="${output}"'<separator label="Files" />
- '
- output="${output}${files_menu}"'
- '; }
- output="${output}"'</openbox_pipe_menu>
- '
- printf '%s' "$output"
- exit
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.