linux-scripts/options/gnome-keybinds.sh
billypom on debian 8db9dab4f8 idk
2025-07-18 21:06:21 -04:00

98 lines
3.5 KiB
Bash
Executable File

#!/bin/bash
# this is the worst script in the world
# "name|command|binding"
keybindings=(
"Terminal|kitty|<Super>Return"
"wallpaper|~/utils/gnome-wallpaper/wallpaper.sh|Launch7"
"wallpaper2|~/utils/gnome-wallpaper/my_wallpaper.sh|<Control>Launch7"
"browser|librewolf|<Super>Shift"
)
# Get the current list of custom keybindings
current_keybindings=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings)
current_keybindings=$(echo "$current_keybindings" | sed "s/^\[\(.*\)\]$/\1/")
if [ "$current_keybindings" == "@as []" ]; then
echo "Current keybinds are empty"
next_index=0
else
# Get the highest index currently used
existing_indices=$(echo "$current_keybindings" | tr ',' '\n' | grep -o 'custom[0-9]\+' | sed 's/custom//' | sort -n)
if [ -z "$existing_indices" ]; then
next_index=0
else
last_index=$(echo "$existing_indices" | tail -n 1)
next_index=$((last_index + 1))
fi
# Store new paths to update the keybinding list
new_keybinding_paths=()
# Get all existing bindings for comparison (just the binding part)
existing_bindings=()
for keybinding in $(echo "$current_keybindings" | tr ',' '\n'); do
binding_path=$(echo "$keybinding" | sed "s/^'//;s/'$//")
binding_command=$(gsettings get org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:$binding_path binding)
existing_bindings+=("$(echo $binding_command | sed "s/^'//;s/'$//")") # Remove quotes
done
fi
# Loop over keybindings and add them
for entry in "${keybindings[@]}"; do
IFS='|' read -r name command binding <<< "$entry"
# Check if the binding already exists
if [ "$current_keybindings" == "@as []" ]; then
echo "Current keybinds are empty"
else
if [[ " ${existing_bindings[@]} " =~ " $binding " ]]; then
# If binding exists, prompt user to overwrite
echo "Keybinding '$binding' already exists for another action."
read -p "Do you want to overwrite it? (y/n): " overwrite
if [[ "$overwrite" != "y" && "$overwrite" != "Y" ]]; then
echo "Skipping '$binding'."
continue
fi
fi
fi
# Define the new keybinding path
new_path="/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$next_index/"
new_keybinding_paths+=("'$new_path'") # Wrap paths in single quotes
# Set individual keybinding settings
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:"$new_path" name "$name"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:"$new_path" command "$command"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:"$new_path" binding "$binding"
echo "Added keybinding: $name ($binding) -> $command"
next_index=$((next_index + 1))
done
# Now merge old and new keybindings properly
# First, join the keybinding paths with commas, ensuring proper quoting
if [ "$current_keybindings" == "@as []" ]; then
echo "Current keybinds are empty"
all_keybindings=""
else
all_keybindings="$current_keybindings"
fi
for path in "${new_keybinding_paths[@]}"; do
# Add each new keybinding path to the list, ensuring proper formatting
if [ -z "$all_keybindings" ]; then
all_keybindings="$path"
else
all_keybindings="$all_keybindings,$path"
fi
done
# Ensure the list is properly wrapped in square brackets
all_keybindings="[$all_keybindings]"
# Update the system keybinding list
gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "$all_keybindings"
echo "Keybindings updated successfully!"