Add script to automate creation of GNOME custom keyboard shortcuts. - Detect existing custom keybindings - Find the next available custom shortcut slot - Register new custom-keybinding entries automatically - Configure shortcut name, command, and key combination - Preserve existing GNOME keyboard shortcuts - Add usage documentation and examples - Include helper notes for discovering key names with xev - Simplify scripting and workstation setup automation
124 lines
3.0 KiB
Bash
Executable File
124 lines
3.0 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
###############################################################################
|
|
# Script Name: add-gnome-shortcut.sh
|
|
#
|
|
# Description:
|
|
# Creates a new custom keyboard shortcut in GNOME by automatically finding
|
|
# the next available custom-keybinding slot and registering the shortcut.
|
|
#
|
|
# The script:
|
|
# 1. Reads existing GNOME custom keyboard shortcuts.
|
|
# 2. Finds the first unused custom<N> entry.
|
|
# 3. Adds the new shortcut to the GNOME keybinding list.
|
|
# 4. Sets the shortcut name.
|
|
# 5. Sets the command to execute.
|
|
# 6. Sets the keyboard shortcut combination.
|
|
#
|
|
# Requirements:
|
|
# - GNOME Desktop Environment
|
|
# - gsettings
|
|
#
|
|
# Usage:
|
|
# ./add-gnome-shortcut.sh <name> <command> <key_combo>
|
|
#
|
|
# Arguments:
|
|
# <name>
|
|
# Display name shown in GNOME Settings.
|
|
#
|
|
# <command>
|
|
# Command that will be executed when the shortcut is pressed.
|
|
#
|
|
# <key_combo>
|
|
# GNOME keybinding string.
|
|
#
|
|
# Examples:
|
|
#
|
|
# Open Terminal:
|
|
# ./add-gnome-shortcut.sh \
|
|
# "Terminal" \
|
|
# "gnome-terminal" \
|
|
# "<Ctrl><Alt>T"
|
|
#
|
|
# Launch Firefox:
|
|
# ./add-gnome-shortcut.sh \
|
|
# "Firefox" \
|
|
# "firefox" \
|
|
# "<Super>F"
|
|
#
|
|
# Notes:
|
|
# - Existing shortcuts are preserved.
|
|
# - The next available custom-keybinding ID is selected automatically.
|
|
# - Shortcut configuration can be viewed in:
|
|
# Settings → Keyboard → Keyboard Shortcuts → Custom Shortcuts
|
|
#
|
|
###############################################################################
|
|
|
|
if [ $# -lt 3 ]; then
|
|
echo "Usage: $0 <new_name> <new_cmd> <key_combo>"
|
|
exit 1
|
|
fi
|
|
|
|
counter=0
|
|
list="["
|
|
num=0
|
|
|
|
# Find next available custom keybinding slot
|
|
while read -r line; do
|
|
list="$list$(echo "$line" | sed 's/\]//g' | sed 's/\[//g'), "
|
|
|
|
ret="$(printf "%s\n" "$line" | grep "custom$counter")"
|
|
|
|
if [ -z "$ret" ]; then
|
|
num=$counter
|
|
break
|
|
else
|
|
((counter++))
|
|
num=$counter
|
|
fi
|
|
done < <(
|
|
gsettings get \
|
|
org.gnome.settings-daemon.plugins.media-keys \
|
|
custom-keybindings \
|
|
| sed 's/, /\n/g'
|
|
)
|
|
|
|
key="org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:"
|
|
custom="/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$num/"
|
|
|
|
if [[ $list = *"@as"* ]]; then
|
|
list="['$custom']"
|
|
else
|
|
list="$list'$custom']"
|
|
fi
|
|
|
|
echo "$list"
|
|
|
|
new_name=$1
|
|
new_cmd=$2
|
|
key_combo=$3
|
|
|
|
echo "$key$custom $new_name $new_cmd $key_combo"
|
|
|
|
# Optional helper for determining key names:
|
|
#
|
|
# key_str="$(xev)"
|
|
# echo "$key_str" \
|
|
# | sed 's/KeyRelease/\nKeyRelease/g' \
|
|
# | sed 's/KeyPress/\nKeyPress/g' \
|
|
# | grep KeyPress \
|
|
# | sed 's/keysym/\nkeysym/g' \
|
|
# | sed 's/same_screen/\nsame_screen/g' \
|
|
# | grep keysym \
|
|
# | grep -o -P '(?<=, ).*(?=\),)' \
|
|
# | sed 's/_L//g' \
|
|
# | sed 's/_R//g'
|
|
|
|
gsettings set \
|
|
org.gnome.settings-daemon.plugins.media-keys \
|
|
custom-keybindings \
|
|
"$list"
|
|
|
|
gsettings set "$key$custom" name "$new_name"
|
|
gsettings set "$key$custom" command "$new_cmd"
|
|
gsettings set "$key$custom" binding "$key_combo" |