Table of Contents

Openbox: Generating a Menu from your Self-Written Scripts

I have a collection of scripts for various tasks, which reside in ~/.bin/ Some of them start graphical programs like games installed with wine and similar stuff. I wanted an easy way to add these scripts to my Openbox menu. Thanks to openbox' pipe-menu functionality this is quite easy to do.

The Menu Script

First lets take a look at the script that generates the menu. It is called scriptmenu.sh and lies alongside my other scripts in ~/.bin:

#!/bin/bash

_SCRIPTDIR="/home/seiichiro/.bin"

for i in ${_SCRIPTDIR}/*.msh
do
        echo -n "$(grep "MCATEGORY" < ${i} | cut -d'=' -f2);" >> /tmp/scriptmenu.$$
        echo -n "$(grep "MNAME" < ${i} | cut -d'=' -f2);" >> /tmp/scriptmenu.$$
        echo "${i}" >> /tmp/scriptmenu.$$
done

sort > /tmp/scriptmenu_s.$$ < /tmp/scriptmenu.$$

cut -d';' -f1 < /tmp/scriptmenu_s.$$ | uniq > /tmp/menucat.$$

echo '<?xml version="1.0" encoding="utf-8"?>'
echo '<openbox_pipe_menu>'
while read line
do
        echo "  <menu id=\"sm-${line}\" label=\"${line}\">"
        IFS=";"
        while read -a line2
        do
                if [ "${line}" == "${line2[0]}" ]
                then
                        echo "  <item label=\"${line2[1]}\">"
                        echo "    <action name=\"Execute\">"
                        echo "      <execute>"
                        echo "        ${line2[2]}"
                        echo "      </execute>"
                        echo "    </action>"
                        echo "  </item>"
                fi
        done < /tmp/scriptmenu_s.$$
        echo "  </menu>"
done < /tmp/menucat.$$
echo '</openbox_pipe_menu>'

rm /tmp/*.$$

Preparing the Scripts for the Menu

Since I don't want all of the scripts in the menu (many of them are terminal-progs without an UI) I thougt about a way to select the wanted scripts. Since my scripts are normally called <something>.sh I name all scripts that shall apear in the menu <something>.msh (for menu-script). A second thing I wanted to have is a posibility to define names and categories for the scripts independent from their filename. So I use 2 “variables” inside the script for defining these. Have a look at my Guild Wars starter script for example:

#!/bin/bash
#MCATEGORY=Spiele
#MNAME=Guild Wars

export WINEPREFIX="/mnt/data/Games/Guildwars"
export WINEDEBUG="-all"
wine C:\\Programme\\GuildWars\\Gw.exe

The important part here are the lines 2 and 3. MCATEGORY defines the Category to use, and MNAME obviously the name for the menu entry. These two lines have to be present in every script or the menu-generation will fail. Everyhing before MCATEGORY/MNAME is ignored (so they can be comments like in the example above), and everything after the “=” will be used as category label or name.

Including it into the Openbox Menu

The last part is encluding the menu-script in the openbox menu as a pipemenu. This is as easy as adding the following line to ~/.config/openbox/menu.xml at the position you want the menu to appear:

<menu execute="/home/seiichiro/.bin/scriptmenu.sh" id="scriptmenu" label="Scriptmenu"/>

Done!

Thats it, here is a screenshot of the script in action. everything below “Scriptmenu” is generated on the fly:

scriptmenu

Disclaimer

Like all my other scripts I present here, I give absolutely no guaranty for the function or correctness of the script. I also can't be held responsible if anything bad happens from using it like destruction of data or similar. Feel free to adapt it to your needs and don't hesitate to use the comments area below for questions or suggestions.