desktop-file
1#!/bin/bash
2
3# Desktop File Script
4#
5# This script generates desktop files for applications.
6# It allows users to customize desktop files for applications.
7#
8# Usage:
9# ./desktop-file - Load the main menu
10# ./desktop-file -h, --help - Display help information
11# ./desktop-file -v, --version - Display version information
12#
13# Author: Stella <choco@choco.rip>
14# Version: 1.0
15# Last Updated: 19/9/2024 05:33 AM JST (GMT+9, Tokyo)
16
17# Variables
18VERSION="1.0"
19LAST_UPDATED="19/9/2024 05:33 AM JST (GMT+9, Tokyo)"
20AUTHOR="Stella <choco@choco.rip>"
21GITHUB="https://github.com/chocoOnEstrogen/dotfiles"
22BUG_REPORT="https://github.com/chocoOnEstrogen/dotfiles/issues"
23
24# Functions
25
26verify_type() {
27 local type=$1
28 local type_expected=$2
29
30 case "$type_expected" in
31 bool)
32 if [[ "$type" == "true" || "$type" == "false" ]]; then
33 return 0
34 else
35 echo "Error: Expected boolean (true/false), got '$type'"
36 return 1
37 fi
38 ;;
39 int)
40 if [[ "$type" =~ ^[0-9]+$ ]]; then
41 return 0
42 else
43 echo "Error: Expected integer, got '$type'"
44 return 1
45 fi
46 ;;
47 string)
48 if [[ -n "$type" ]]; then
49 return 0
50 else
51 echo "Error: Expected non-empty string"
52 return 1
53 fi
54 ;;
55 *)
56 echo "Error: Unknown type expected '$type_expected'"
57 return 1
58 ;;
59 esac
60}
61
62mk_desktop_file() {
63 local name=$1
64 local exec_path=$2
65 local icon_path=$3
66 local terminal=$4
67 local categories=$5
68
69 if [ -z "$name" ] || [ -z "$exec_path" ] || [ -z "$icon_path" ]; then
70 echo "Error: Missing arguments."
71 return 1
72 fi
73
74 verify_type "$terminal" "bool" || return 1
75
76 local desktop_file="$HOME/.local/share/applications/${name}.desktop"
77
78 cat > "$desktop_file" << EOF
79[Desktop Entry]
80Version=1.0
81Type=Application
82Name=$name
83Exec=$exec_path
84Icon=$icon_path
85Terminal=$terminal
86Categories=$categories
87EOF
88
89 echo "Desktop file created: $desktop_file"
90}
91
92# Main Menu
93main_menu() {
94 while true; do
95 echo "Desktop File Generator"
96 echo "1. Create new desktop file"
97 echo "2. Edit existing desktop file"
98 echo "3. Delete desktop file"
99 echo "4. Exit"
100 read -p "Choose an option: " choice
101
102 case $choice in
103 1) create_desktop_file ;;
104 2) edit_desktop_file ;;
105 3) delete_desktop_file ;;
106 4) exit 0 ;;
107 *) echo "Invalid option. Please try again." ;;
108 esac
109 done
110}
111
112# Create desktop file
113create_desktop_file() {
114 read -p "Enter application name: " name
115 read -p "Enter executable path: " exec_path
116 read -p "Enter icon path: " icon_path
117 read -p "Run in terminal? (true/false): " terminal
118 read -p "Enter categories (semicolon-separated): " categories
119
120 mk_desktop_file "$name" "$exec_path" "$icon_path" "$terminal" "$categories"
121}
122
123# Edit desktop file
124edit_desktop_file() {
125 local desktop_files_dir="$HOME/.local/share/applications"
126
127 # Use newline as delimiter instead of spaces
128 IFS=$'\n'
129 local files=($(ls "$desktop_files_dir"/*.desktop))
130
131 if [ ${#files[@]} -eq 0 ]; then
132 echo "No desktop files found."
133 return
134 fi
135
136 echo "Select a desktop file to edit:"
137 for i in "${!files[@]}"; do
138 echo "$((i+1)). $(basename "${files[$i]}")"
139 done
140
141 read -p "Enter the number of the file to edit: " file_num
142 if [[ "$file_num" =~ ^[0-9]+$ ]] && [ "$file_num" -ge 1 ] && [ "$file_num" -le "${#files[@]}" ]; then
143 ${EDITOR:-nano} "${files[$((file_num-1))]}"
144 else
145 echo "Invalid selection."
146 fi
147 unset IFS
148}
149
150
151
152# Delete desktop file
153delete_desktop_file() {
154 local desktop_files_dir="$HOME/.local/share/applications"
155
156 # Use newline as delimiter instead of spaces
157 IFS=$'\n'
158 local files=($(ls "$desktop_files_dir"/*.desktop))
159
160 if [ ${#files[@]} -eq 0 ]; then
161 echo "No desktop files found."
162 return
163 fi
164
165 echo "Select a desktop file to delete:"
166 for i in "${!files[@]}"; do
167 echo "$((i+1)). $(basename "${files[$i]}")"
168 done
169
170 read -p "Enter the number of the file to delete: " file_num
171 if [[ "$file_num" =~ ^[0-9]+$ ]] && [ "$file_num" -ge 1 ] && [ "$file_num" -le "${#files[@]}" ]; then
172 rm "${files[$((file_num-1))]}"
173 echo "File deleted: ${files[$((file_num-1))]}"
174 else
175 echo "Invalid selection."
176 fi
177 unset IFS
178}
179
180
181
182# Run the main menu
183main_menu