Created
March 7, 2020 15:22
-
-
Save mariovalney/b5332dd2606b5c3352ca24e3f5bb6045 to your computer and use it in GitHub Desktop.
Creates a desktop entry on /usr/share/applications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
# Common variables | |
BL=$'\n' | |
SELECT_YES="YES" | |
SELECT_NO="NO" | |
SELECT_OVERWRITE="Overwrite" | |
SELECT_EXIT="Exit" | |
SELECT_KEEP="Use current icon" | |
# Args | |
ARG_NAME='' | |
ARG_COMMAND='' | |
ARG_ICON='' | |
print_usage() { | |
echo "Usage: $0 -n [Example] -c [/path/to/command] -i [https://www.example.com/icon.png]" | |
exit 1 | |
} | |
while getopts 'n:c:i:' flag; do | |
case "${flag}" in | |
n) ARG_NAME="${OPTARG}" ;; | |
c) ARG_COMMAND="${OPTARG}" ;; | |
i) ARG_ICON="${OPTARG}" ;; | |
*) print_usage | |
exit 1 ;; | |
esac | |
done | |
# Checking variables | |
if [[ $ARG_NAME == '' || $ARG_COMMAND == '' ]]; then | |
print_usage | |
exit | |
fi | |
# Requesting SUDO | |
sudo -v | |
SLUG=$(echo "$ARG_NAME" | sed -r 's/([A-Z])/\L\1/g' | sed -r 's/(\s)/-/g') | |
DOWNLOADED_FILE=$(basename "$ARG_ICON") | |
EXTENSION="${DOWNLOADED_FILE##*.}" | |
ICON_FILE="/usr/share/pixmaps/$SLUG.$EXTENSION" | |
SHORTCUT_FILE="/usr/share/applications/$SLUG.desktop" | |
echo "$BL------------------------" | |
echo "- Starting application -" | |
echo "------------------------$BL" | |
# Creating shortcut file | |
if [ -f $SHORTCUT_FILE ] | |
then | |
echo "$BL Desktop entry already exists:" | |
select answer_entry in "$SELECT_OVERWRITE" "$SELECT_EXIT"; do | |
case $answer_entry in | |
$SELECT_OVERWRITE) break;; | |
$SELECT_EXIT) exit;; | |
esac | |
done | |
fi | |
# Function to download icon | |
download_icon() { | |
echo "$BL Downloading icon..." | |
wget -q $ARG_ICON | |
sudo mv $DOWNLOADED_FILE $ICON_FILE | |
} | |
if [[ $ARG_ICON == '' ]]; then | |
echo "$BL No icon provided. Filling with the name." | |
else | |
# Checking for icon | |
if [ -f $ICON_FILE ] | |
then | |
echo "$BL Icon already exists on /usr/share/pixmaps:" | |
select answer_icon in "$SELECT_OVERWRITE" "$SELECT_KEEP"; do | |
case $answer_icon in | |
$SELECT_OVERWRITE) download_icon; break;; | |
$SELECT_KEEP) break;; | |
esac | |
done | |
else | |
download_icon | |
fi | |
fi | |
# Creating desktop entry | |
echo "$BL Creating desktop entry..." | |
echo "[Desktop Entry] | |
Name=$ARG_NAME | |
Comment=$ARG_NAME Shortcut (usage: $SLUG to run $ARG_COMMAND) | |
Exec=$ARG_COMMAND | |
Icon=$SLUG | |
Type=Application" | sudo tee $SHORTCUT_FILE > /dev/null | |
open_file() { | |
if [ -x "$(command -v subl)" ]; then | |
sudo subl $SHORTCUT_FILE | |
return | |
fi | |
if [ -x "$(command -v nano)" ]; then | |
sudo nano $SHORTCUT_FILE | |
return | |
fi | |
if [ -x "$(command -v vim)" ]; then | |
sudo vim $SHORTCUT_FILE | |
return | |
fi | |
echo "$BL Impossible to find a file editor. We tried: subl / nano / vim." | |
} | |
echo "$BL Finished: open file to edit?" | |
select answer_edit in "$SELECT_YES" "$SELECT_NO"; do | |
case $answer_edit in | |
$SELECT_YES) open_file; break;; | |
$SELECT_NO) break;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment