showmenu - displays content of .desktop files.

showmenu is a simple script which prints the content of application .desktop files to standard output (after removing the locale stuff). These files - located by default in /usr/share/applications - are used by XDG compliant desktops - such as KDE, Gnome, XFCE and LXDE - to build the menu entries.

The script is useful if you wonder where an application should appear in the menu (this info is given by the ‘Categories’ key), why it doesn’t show up (check the OnlyShowIn and NotShowIn, as well as the Hidden keys), what’s the command that starts the application (Exec key), icon (‘Icon’ key), etc.

Here’s the code:


#! /bin/bash
#: Title       : showmenu
#: Date Created: Mon Jul 16 18:19:40 PDT 2012 
#: Last Edit   : Tue Aug  7 17:52:13 PDT 2012
#: Author      : Agnelo de la Crotche (please_try_again)
#: Version     : 1.1
#: Description : search and display desktop files.
#: options     : -a or --autostart : look for autostarted applications 
#              : -n or --noheaders : don't print menu name - if you want to redirected the output into a .desktop file. 
#              : this option will be ignored if there are more than one .desktop file to display.
#              : -e or --exec      : show only the command (the value of Exec)
#              : also ignore if there are more than one menu

prg=$(basename $0)


function syntax {
exec cat << EOFSYNTAX
$prg - show content of menu entries (.desktop files)

usage:   $prg [options] 

options: 
       -h --help                 Display this help.
       -n --noheaders            Don't print header/footer lines
       -a --autostart            Show autostarted applications only
       -d --default              Look for applications in /usr/share/applications only
       -e --exec                 Display only Exec value    
EOFSYNTAX
exit
}


args=`getopt -q -u -o hande -l help,autostart,noheaders,default,exec -- "$@"`
set -- $args
for i; do
    case "$i" in
        -h|--help) syntax ;;
        -a|--autostart)
            USER_APPSDIR=~/.config/autostart
            SYS_APPSDIR=/etc/xdg/autostart
            shift
        ;;
        -d|--default)
            USER_APPSDIR=$HOME/local/.share
            SYS_APPSDIR=/usr/share/applications
            shift
        ;;
        -n|--noheaders)
            NOTITLE=yes
            shift
        ;;
        -e|--exec)
            NOTITLE=yes
            EXEC=yes
            shift
        ;;
    esac
done

shift

 "$1" ] || exec echo "missing argument"
 $# -gt 1 ] && exec echo "too many arguments"

app=$1

 "$USER_APPSDIR" ] || USER_APPSDIR=${XDG_DATA_HOME:-~/local/share}/applications
 "$SYS_APPSDIR"  ] || SYS_APPSDIR=$(echo ${XDG_DATA_DIRS:-/usr/local/share:/usr/share} | sed 's|:|/applications |g;s|$|/applications|;s|//|/|g')


APPSDIR="${USER_APPSDIR} ${SYS_APPSDIR}"

menu=""
for d in $(echo $APPSDIR | tr ":" " ") ; do
    if  -d $d ]; then
        menu=$(find $d -name "X_$app.desktop" -o -name "$app.desktop" | head -1)
    fi
    if  "$menu" ]; then
         "$NOTITLE" ] || printf "*** %s ***
" "$menu"
         "$EXEC" ] && sed -n 's/^Exec=//p' $menu || sed '/\^]]*\]=/d' $menu
         "$NOTITLE" ] || printf "*** *** ***
"
        break
    fi
done

 "$menu" ] && exit

for d in $(echo $APPSDIR | tr ":" " ") ; do
    m=""
    if  -d $d ]; then
        "$m" ] || m=$(find $d -name "X_$app*.desktop" -o -name "$app*.desktop" )
    fi
     "$m" ] && menu="$menu $m"
done

 "$menu" ] || exec printf "No menu file matching %s.
" $app

menu=$(echo $menu | tr " " "
 " | sed 's|\(.*\)/\(.*\)|\2 \1|;s|^X_\(^ ]*\)|\1 X_|' | sort | sed 's|\(^ ]*\) X_|X_\1|;s|\(^ ]*\) \(.*\)|\2/\1|')

for m in $menu; do
    printf "*** %s ***
" "$m"
    sed '/\^]]*\]=/d' $m
    printf "*** *** ***

" 
done

I wrote this script for my systems. That’s why the latest sed pipe looks so strange. But it will work on uncustomized systems too (any distro). You can list the few options with:

$ showmenu -h

A couple examples:


$ showmenu firefox
*** /usr/local/share/applications/X_firefox.desktop ***
[Desktop Entry]
X-SuSE-translate=true
Categories=Network;WebBrowser;GTK;X-Communication-Browsers;
Encoding=UTF-8
Name=Firefox
GenericName=Web Browser
Comment=Web Browser
TryExec=firefox
Exec=firefox %u
Icon=firefox
Terminal=false
StartupNotify=true
MimeType=text/html;text/xml;application/xhtml+xml;application/vnd.mozilla.xul+xml;text/mml;application/x-xpinstall;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/ftp;
Type=Application
*** *** ***

$ showmenu -d firefox
*** /usr/share/applications/firefox.desktop ***
[Desktop Entry]
X-SuSE-translate=true
Categories=Network;WebBrowser;GTK;
Encoding=UTF-8
Name=Firefox
GenericName=Web Browser
Comment=Web Browser
TryExec=firefox
Exec=firefox %u
Icon=firefox
Terminal=false
StartupNotify=true
MimeType=text/html;text/xml;application/xhtml+xml;application/vnd.mozilla.xul+xml;text/mml;application/x-xpinstall;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/ftp;
Type=Application

*** *** ***

This first command looks in the directories defined in XDG_DATA_DIRS (that you might change to keep your customized .desktop files in another location - I put mine in /usr/local/share). The second only looks in the default location (/usr/share) - more precisely in the ‘applications’ subdirectoy of /usr/local/share and /usr/share, but you don’t specify ‘applications’ in XDG_DATA_DIRS. These two commands will probably print the same output on your system.


$ echo $XDG_DATA_DIRS
/usr/local/share:/usr/share:/etc/opt/kde3/share:/opt/kde3/share

  • I don’t know where this /etc/opt/kde3 comes from yet - but it’s not important.

$ showmenu -e firefox
firefox %u

only shows the command which runs the application.

  • showmenu -a looks for .desktop files in /etc/xdg/autostart and ~/.config/autostart.
  • showmenu -n don’t print headers. Use this option to redirect the output to a new .desktop file.

showmenu will also try to complete the argument or display all matching entries. So if you type ‘showmenu gnome’, the output might get pretty long.

On 2012-08-09 03:56, please try again wrote:
>
> showmenu is a simple script which prints the content of application
> desktop files to standard output (after removing the locale stuff).
> These files - located by default in /usr/share/applications - are used
> by XDG compliant desktops - such as KDE, Gnome, XFCE and LXDE - to build
> the menu entries.

I intend to try this, but I keep having to delay it. I unintentionally destroyed my main system
(deleted /var and many more files), had to recover it. Spent lot of time. System is up again
but I still have work to do on it, and then some real life work that I hate like gardening and
in august.


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” at Telcontar)