Skip to content

Instantly share code, notes, and snippets.

@nealian
Last active April 5, 2017 18:33
Show Gist options
  • Save nealian/0b541c010372f9e638e8500381e5791e to your computer and use it in GitHub Desktop.
Save nealian/0b541c010372f9e638e8500381e5791e to your computer and use it in GitHub Desktop.
A but.sh template to generate a .desktop file

.desktop file buttemplate

This is a but.sh template for creating .desktop files, which are used for menu entries and links in most Linux desktop environments. Built against the specification

Prerequisites

This, of course, requires but.sh The template itself requires only bash and gnu-coreutils.

Variables

Required:

  • ENTRY_TYPE -- one of Application, Link, or Directory
  • ENTRY_NAME -- intended name
  • ENTRY_URL* -- the URL that the Link should point to
    • * required only for Link type

Optional:

  • ENTRY_VERSION -- the version of the desktop entry specification that this implements
  • ENTRY_GENERIC_NAME -- generic name (eg. "Web Browser" or "Mail Client")
  • ENTRY_NAME_<locale> -- intended name for the given locale
  • ENTRY_GENERIC_NAME_<locale> -- generic name for the given locale
  • ENTRY_NO_DISPLAY -- boolean value that determines whether the entry is displayed in menus.
  • ENTRY_COMMENT --
  • ENTRY_COMMENT_<locale> --
  • ENTRY_ICON --
  • ENTRY_ICON_<locale> --
  • ENTRY_HIDDEN --
  • ENTRY_ONLY_SHOW_IN --
  • ENTRY_NOT_SHOW_IN --
  • ENTRY_DBUS_ACTIVATABLE --
  • ENTRY_TRY_EXEC --
  • ENTRY_EXEC --
  • ENTRY_PATH --
  • ENTRY_TERMINAL --
  • ENTRY_ACTIONS --
  • ENTRY_MIME_TYPE --
  • ENTRY_CATEGORIES --
  • ENTRY_IMPLEMENTS --
  • ENTRY_KEYWORDS --
  • ENTRY_KEYWORDS_<locale> --
  • ENTRY_STARTUP_NOTIFY --
  • ENTRY_STARTUP_WM_CLASS --

Downloading

wget https://gist.github.com/nealian/0b541c010372f9e638e8500381e5791e/raw/desktop.buttemplate{,.functions}

Usage

but.sh desktop.buttemplate -c desktop.buttemplate.functions -c app.environment -o /usr/share/applications/app.desktop
#!/usr/bin/env xdg-open
[Desktop Entry]
Type=${ENTRY_TYPE}
$(_print_key_val ENTRY_VERSION Version;
locales="${!ENTRY_NAME[@]}"; for locale in "${locales:-0}"; do echo "Name$([ "$locale" = "0" ] || echo "[$locale]")=${ENTRY_NAME[$locale]}"; done;
_printall_of_key ENTRY_GENERIC_NAME GenericName;
_print_key_val ENTRY_NO_DISPLAY NoDisplay;
_printall_of_key ENTRY_COMMENT Comment;
_printall_of_key ENTRY_ICON Icon;
_print_key_val ENTRY_HIDDEN Hidden;
_print_key_val ENTRY_ONLY_SHOW_IN OnlyShowIn;
_print_key_val ENTRY_NOT_SHOW_IN NotShowIn;
_print_key_val ENTRY_DBUS_ACTIVATABLE DBusActivatable;
_print_key_val ENTRY_TRY_EXEC TryExec;
_print_key_val ENTRY_EXEC Exec;
_print_key_val ENTRY_PATH Path;
_print_key_val ENTRY_TERMINAL Terminal;
_print_key_val ENTRY_ACTIONS Actions;
_print_key_val ENTRY_MIME_TYPE MimeType;
_print_key_val ENTRY_CATEGORIES Categories;
_print_key_val ENTRY_IMPLEMENTS Implements;
_printall_of_key ENTRY_KEYWORDS Keywords;
_print_key_val ENTRY_STARTUP_NOTIFY StartupNotify;
_print_key_val ENTRY_STARTUP_WM_CLASS StartupWMClass;
_print_key_val ENTRY_URL URL)
function _print_if_var_defined() {
local varname=$1
shift
local delimiter=$1
shift
[ -n "${!varname}" ] && echo "${@}${delimiter}${!varname}"
}
function _print_key_val() {
local varname=$1
shift
_print_if_var_defined "$varname" = "$@"
}
function _printall_of_key() {
local varbasename=$1 key=$2
local var cur
declare -A var
for name in $(eval "echo \${!${varbasename}@}"); do
cur=${name##${varbasename}}
cur=${cur#_}
cur=${cur:-0}
eval $(echo var[${cur}]=\${!name})
done
if [ "0${!var[*]}" = "0" -o "${!var[*]}" = "0" ]; then
_print_if_var_defined $varname = $key
else
for locale in ${!var[@]}; do
if [ -n "${var[$locale]}" ]; then
[[ "$locale" = "0" ]] && echo "${key}=${var[${locale}]}" || echo "${key}[${locale}]=${var[${locale}]}"
fi
done
fi
}
function _exclude() {
local a=$2
for (( i=0; i<${#1}; i++ )); do
a=${a//${1:$i:1}/}
done
echo $a
}
function _split_chars() {
for (( i=0; i<${#1}; i++ )); do
echo -n "${1:$i:1}${2}"
done | head -c -${#2}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment