#!/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 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 # # Arguments: # # Display name shown in GNOME Settings. # # # Command that will be executed when the shortcut is pressed. # # # GNOME keybinding string. # # Examples: # # Open Terminal: # ./add-gnome-shortcut.sh \ # "Terminal" \ # "gnome-terminal" \ # "T" # # Launch Firefox: # ./add-gnome-shortcut.sh \ # "Firefox" \ # "firefox" \ # "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 " 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"