#!/usr/bin/perl## OpenBox Applications (Pipe)Menu (C) Biffidus 2008## (only kidding, it's not copyrighted. Do whatever you like with it)## This pipe menu creates an application menu from the .desktop files# found in many linux distributions. The desktop file must be of type# Application and specify their Categories.## One or more paths to search may be provided on the command line or# specified below. The menu structure is derived from the Categories# attribute and may be customised below with the following keywords:## hide: menu will not be displayed# collapse: the submenus will be used instead## Any item starting with X- is automatically hiddenmy$APPSDIR="/usr/share/applications/ /usr/kde/3.5/share/applications/kde/ ~/.gnome/apps/ ~/.kde/share/apps";my%CFG=("Application"=>"collapse","GTK"=>"collapse","KDE"=>"collapse","Qt"=>"collapse",);################################################################################ Hash of hashes# $Hash{x}->{y} = z# $Hash{x}{y}my%menu;# application details# Hash of arrays# $Hash{$key} = \@Array# @Array = @{$Hash{$key}}my%cat;# apps in each category (hashed lists)################################################### Search for Applications ###if($#ARGV!=-1){$APPSDIR=join(" ",@ARGV);}printSTDERR"Searching $APPSDIR\n";open(APPS,"find ".$APPSDIR." -name '*.desktop' |")||do{printqq|<openbox_pipe_menu><item label="Error!" /></openbox_pipe_menu>\n|;die"could not open $APPSDIR";};@FILES=<APPS>;closeAPPS;######################################################## Parse Applications ###printSTDERR"\n### Scanning applications ###\n";foreach$entry(@FILES){if(open(DE,$entry)&&$entry=~s|^.*/(.*?)\.desktop|$1|){while(<DE>){chomp;if(/^(\w+)=([^%]+)/){# populate application details$menu{$entry}->{$1}=$2;# determine categoryif($1eq"Categories"){printSTDERR$2.$entry;my@tmp=split(/;/,$2);while($CFG{$tmp[0]}eq"collapse"){shift@tmp;}if($CFG{$tmp[0]}ne"hide"&&$tmp[0]!~/^X-/){push@{$cat{$tmp[0]}},$entry;}}}}}}############################################################# Generate Menu ###printSTDERR"\n### Generating menu ###\n";print"<openbox_pipe_menu>\n";foreach$key(sortkeys%cat){printqq| <menu id="obam-$key" label="$key">\n|;foreach$app(sort@{$cat{$key}}){if($menu{$app}{Type}eq"Application"){my$Name=$menu{$app}{Name};my$Exec=$menu{$app}{Exec};printqq| <item label="$Name"><action name="Execute"><execute>$Exec</execute></action></item>\n|;}}print" </menu>";}print"</openbox_pipe_menu>\n";printSTDERR"Done\n";###############################################################################