Skip to content

Instantly share code, notes, and snippets.

@mnesarco
Created December 27, 2020 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnesarco/6e5712ef836bfdd330178ff2bc9be54c to your computer and use it in GitHub Desktop.
Save mnesarco/6e5712ef836bfdd330178ff2bc9be54c to your computer and use it in GitHub Desktop.
linuxdeploy plugin to extract AppImage icon and set it as the AppImage Icon on XDG supported platforms.
#!/bin/bash
# ------------------------------------------------------------------------------
# Install:
# 1. Put this script alongside with linuxdeploy or in the PATH
# 2. make it executable (chmod +x linuxdeploy-plugin-xdg)
#
# Usage:
# 1. Set the env variable XDG_ICON to your png icon.
# 2. Call with linuxdeploy
#
# Example:
# export XDG_ICON=/path/to/my/png/icon
# linuxdeploy --appdir=[...] -d [...] -i [...] --plugin xdg --output appimage
# ------------------------------------------------------------------------------
# --------------------------------------------------
# linuxdeploy plugin api
# --------------------------------------------------
while [ "$1" != "" ]; do
PARAM=`printf "%s\n" $1 | awk -F= '{print $1}'`
VALUE=`printf "%s\n" $1 | sed 's/^[^=]*=//g'`
if [[ $VALUE == $PARAM ]]; then
shift
VALUE=$1
fi
# echo "$PARAM = $VALUE"
case $PARAM in
--appdir)
ARG_APP_DIR=$VALUE
;;
--plugin-api-version)
ARG_PLUGIN_API_VERSION=1
;;
--plugin-type)
ARG_PLUGIN_TYPE=1
;;
*)
echo "[xdg] ERROR: unknown parameter \"$PARAM\""
usage
exit 1
;;
esac
shift
done
if [[ ! -z "$ARG_PLUGIN_API_VERSION" ]]; then
echo "0"
exit 0
fi
if [[ ! -z "$ARG_PLUGIN_TYPE" ]]; then
echo "input"
exit 0
fi
if [[ -z "$ARG_APP_DIR" ]]; then
echo "[xdg] --appdir is required"
exit 1
fi
# --------------------------------------------------
# plugin parameters
# --------------------------------------------------
if [[ -z "$XDG_ICON" ]]; then
XDG_ICON="$PWD/xdg-icon.png"
fi
echo "[xdg] XDG_ICON=$XDG_ICON"
if [[ ! -f "$XDG_ICON" ]]; then
echo "[xdg] $XDG_ICON does not exists"
exit 1
fi
if [[ ! $XDG_ICON == *png ]]; then
echo "[xdg] xdg icon must be in png format (suggested size: 128x128)"
exit 1
fi
mkdir -p "$ARG_APP_DIR/apprun-hooks"
HOOK="$ARG_APP_DIR/apprun-hooks/xdg-icon-hook.sh"
echo "[xdg] Deploying xdg icon $XDG_ICON"
cp "$XDG_ICON" "$ARG_APP_DIR/xdg-icon.png"
# --------------------------------------------------
# Hook script
# --------------------------------------------------
echo "[xdg] Deploying xdg-icon-hook.sh"
cat > $HOOK <<- EOM
# Generated by linuxdeploy-plugin-xdg
if [ ! -z "\$APPIMAGE" ] && [ ! -z "\$APPDIR" ]; then
if [[ -z "\$XDG_CACHE_HOME" ]]; then
XDG_CACHE_HOME="\$HOME/.cache/thumbnails/normal"
fi
if [[ ! -d "\$XDG_CACHE_HOME" ]]; then
mkdir -p "\$XDG_CACHE_HOME"
fi
MD5=\$(echo -n "file://\$APPIMAGE" | md5sum | cut -d' ' -f1)
cp "\$APPDIR/xdg-icon.png" "\$XDG_CACHE_HOME/\$MD5.png"
xdg-icon-resource forceupdate 1>/dev/null 2>&1 &
fi
EOM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment