Topic: Bash (now Dash) "places" openbox pipe menu
Since I moved a lot of my stuff onto a separate "data" partition, symlinked from my /home/john folder I discovered the Crunchbang Openbox "places" menu doesn't follow symlinks, despite its claims to do so. Apostrophes in path names also cause trouble. Had a look at the code but found it was perl, so to try and tweak it would require learning perl first...
So I put together this bash (now dash) version. On my system at least, it seems to follow symlinks OK, and handles filenames with breaks, quotes or "&"s in them. As a bonus, you can set it to open executables in a text editor instead of executing them, likewise .desktop files, and add any other special conditions you want to overrule the default action which is set by Thunar (or pcmanfm or ... ). It's possible to have some dotfiles/dotfolders visible if you want. Usage is exactly the same as the default Crunchbang /usr/bin/crunchbang/obpipemenu-places script (it's in ~/.config/openbox/pipemenus in Statler I think), so the easiest way to install it might be to rename the old file and replace it with this:
EDIT: The latest version will always be in the pastebin, so you no longer need to scroll down the thread, unless you're interested in the bugs that came up...
EDIT: You now have the option of adding a "recently opened files" submenu at the top of the places menu if you want. Make sure you have the recently_opened_menu.sh script, add its path to the places script in the place shown in the comments, and edit the entry in menu.xml to add the --recent option:
<menu id="places" label="Places" execute="/path/to/dash_places_menu.sh --recent ~/" />EDIT 2012/12/17: The comments have been edited: Openbox 3.5 needs the user's home directory to be written as '~/' not '~'. Also the default text editor set to geany instead of gedit.
------------------------------------------------------------------------------------
(This version is old - use the pastebin link above.)
#!/bin/bash
# bash places menu for openbox
#### Usage: add
# <menu id="places" label="Places" execute="/path/to/bash_places_menu.sh ~" />
# to your .config/openbox/menu.xml (you don't need a final slash after the ~)
# Default command to open files or folders with
open_cmd=thunar
# Your favourite text editor
text_editor=gedit
# function to overrule default open command for certain files - add other conditions to choice
open_file_cmd() {
[[ -x $1 ]] && { echo "$text_editor"; return; } # comment out this line if you don't want to edit executables instead of executing
#[ -x "$1" ] && { echo "terminator -e"; return; } # uncomment this and comment out previous line to run executables in terminal instead of editing
[[ $1 = *.desktop ]] && { echo "$text_editor"; return; } # comment out this line if you don't want to edit .desktop files instead of executing
echo "$open_cmd" # default open command
}
path="${1:-$HOME}" # default place is ~/
escape() { # escape xml special characters
string="${1//&/&}"
string="${string//</<}"
string="${string//>/>}"
string="${string//\"/"}"
string="${string//\'/'}"
echo "$string" ;
}
pathe="$( escape "$path" )"
echo "<openbox_pipe_menu>
<item label=\"Browse here..\">
<action name=\"Execute\">
<execute>
$open_cmd \"$pathe\"
</execute>
</action>
</item>
<separator />"
shopt -s nullglob #dotglob # uncomment "dotglob" to see dot files/folders, here & "shopt" line below
for i in "$path"/*
do
ie="$( escape "$i" )"
shortname="${ie##*/}"
[[ -d $i ]] && { echo "<menu id=\"$ie"\ label=\"$shortname\" execute=\""$0" "$ie"\" />"; continue; }
echo "<item label=\"$shortname\">
<action name=\"Execute\">
<execute>
$(open_file_cmd "$i") \"$ie\"
</execute>
</action>
</item>"
done
shopt -u nullglob #dotglob
echo "</openbox_pipe_menu>"
exit(edit: 7/22 Unquoted the commands on lines 34 & 49 so commands like 'terminator -e" will work.)
(edit: 8/13 Cleaned up quotes a bit.)
The hardest part was escaping and quoting, which cost me a couple of fun-filled hours, but now it seems to be working OK. Please give it a try and post if you find any bugs, or have ideas for improvements. ![]()
Last edited by johnraff (2012-02-17 13:23:07)
------------------------
( a boring Japan blog , and idle twitterings )
“There is more Unix-nature in one line of shell script than there is in ten thousand lines of C.” - Master Foo