Mod and hack the GNome main menu with automated scripts to create a menu of the directory.

Problem:
I had a directory with very good collection of my scripts and very organized in folders. The structure is something like this, first I have the broad classification of the category. Then I have the name of the tool/script as the name of the directory. All these are perl/python or shell scripts and luckily labelled so with the extension. I want to add these to the GNome main (Applications) menu.
Solution:
Lets take it stepwise:
1)  Add new menu tools to the Applications menu. Go to /etc/xdg/menus
Open the file applications.menu and add the following before <DefaultMergeDirs/>

<!– Test  –>
<Menu>
<Name>Test</Name>
<!– Scan legacy dirs first, as later items take priority –>
<LegacyDir>/usr/share/applications/test </LegacyDir>
<Directory>X-GNOME-test.directory</Directory>
<OnlyUnallocated/>
<Include>
<And>
<Category>database</Category>
</And>
</Include>
</Menu> <!– End test –>

2) Now go to directory \”/etc/xdg/menus/applications-merged\” and create a new file called \”test-categories.menu\” and put the following in the file:

<Menu>
<Name>Applications</Name>

<Menu>
<Name>Pentest</Name>
<Menu>
<Name>database</Name> <!– name of the directory in menu –>
<Directory>pentest-database.directory</Directory>
<Include>
<And>
<Category>database</Category> <!– This is the category that you need to add in the desktop file.–>
</And>
</Include>
</Menu>

</Menu>
</Menu>

You can include as many directory as you want.

3) Now the fun part of creating the desktop files that link to the perl/python or the shell scripts and actually populate the menu. Note: Until the menu contains any item, the menu will not appear. Okay, go to directory \”/usr/share/applications/test\”. Create a shell script with  the following and run it in the directory containing your scripts, and then \”mv *.desktop /usr/share/applications/test\”:

for i in $(find . -type f -executable)
do

##Get the directory name from the full path
dir=$(dirname $i)
cat=$(echo $dir|awk -F/ \'{print $2}\’)
name=$(echo $dir|awk -F/ \'{print $3}\’)

## Find the type of the file
[[ \”${i}\”  =~ sh$ ]] && type=sh;
[[ \”${i}\”  =~ pl$ ]] && type=pl;
[[ \”${i}\”  =~ py$ ]] && type=py;
if [[ \”$type\” = \”pl\” ]]
then
exec=perl
elif [[ \”$type\” = \”sh\” ]]
then
exec=bash
else
exec=python
fi
[[ \”${type}\” =~ ^$ ]] || (
##echo \”creating file\”
cat <<EOF >test_$(basename $i).desktop

[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=$name – $(basename $i $type)
GenericName=$name – $(basename $i $type)
Exec=xterm -title \”$name – $(basename $i)\” -e \’$exec /tools/pentest/$dir/$(basename $i)\’
Terminal=true
Categories=$cat
EOF
)
##echo $i $dir $cat $type
unset type
done

And you are done.. Do leave me a comment if you like it.

\"\"