Skip to content

Instantly share code, notes, and snippets.

@gmammolo
Created April 8, 2024 17:17
Show Gist options
  • Save gmammolo/eafebf21b47238a492c521bda3b20145 to your computer and use it in GitHub Desktop.
Save gmammolo/eafebf21b47238a492c521bda3b20145 to your computer and use it in GitHub Desktop.
This is a Bash script for generating a launcher in Linux. It takes command line arguments for the filename, command, description, and path of the launcher. The script is designed to create a launcher in the /usr/local/share/applications/ directory.
#!/bin/bash
# genera un launcher
#
# Folder
#
folder=/usr/local/share/applications/
# Parse command line arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-f|--filename)
filename="$2"
shift
shift
;;
-c|--command)
command="$2"
shift
shift
;;
-d|--description)
description="$2"
shift
shift
;;
-p|--path)
path="$2"
shift
shift
;;
-i|--icon)
icon="$2"
shift
shift
;;
-h|--help)
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " -f, --filename Specify the filename*"
echo " -c, --command Specify the command*"
echo " -p, --path If is require a working directory (optional)"
echo " -d, --description Specify the description (optional)"
echo " -i, --icon Specify the icon path (optional)"
echo " -h, --help Show this help message (optionale)"
exit 0
;;
*)
echo "Invalid option: $key"
exit 1
;;
esac
done
# This script generates a new launcher.
echo "Script that generates a new launcher"
# se non sono stati inseriti dei parametri, avvia la versione interattiva
if [ -z "$filename" ] || [ -z "$command" ]; then
read -p "Enter the filename*: " filename
read -p "Enter the description[optional]: " description
read -p "Enter the command*: " command
read -p "Enter the path (or leave blank for default)[optional]: " path
read -p "Enter the icon path (or leave blank for default)[optional]: " icon
fi
# verifica che sia solo un file e non un path. in caso di path dai errore ed esce
if [[ $filename == */* ]]; then
echo "Invalid filename. Please provide only the name without any folder or subfolder references."
exit 1
fi
# se il filename non ha il .desktop finale, aggiungilo
if [[ ! $filename == *.desktop ]]; then
filename="$filename.desktop"
fi
if [ -n "$icon" ]; then
icon_line="Icon=$icon"
else
icon_line="Icon="
fi
if [ -n "$path" ]; then
path_line="Path=$path"
else
path_line=""
fi
if [ -e "$folder$filename" ]; then
echo "File already exists."
read -p "File already exists. Do you want to overwrite it? (y/n): " overwrite
if [[ "$overwrite" =~ ^[yYsS]$ ]]; then
echo "the file will be overwritten..."
mv "$folder$filename" "$folder$filename.$(date +%Y%m%d%H%M%S)"
else
echo "File not created. Exiting..."
exit 0
fi
fi
if [ -z "$filename" ] || [ -z "$command" ]; then
echo "Filename or command is missing. Exiting..."
exit 1
fi
# Create the .desktop file
sudo bash -c "cat << EOF > \"$folder$filename\"
#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Type=Application
Name=$filename
Comment=$description
$path_line
Exec=$command
$icon_line
Terminal=false
Categories=Utility;
EOF"
echo "done!"
sudo chmod +x $folder$filename
echo "creato file in $folder$filename"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment