Files
I38/scripts/bind_to_scratchpad.sh
2025-12-30 16:52:00 -05:00

63 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# This file is part of I38.
# I38 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
# I38 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with I38. If not, see <https://www.gnu.org/licenses/>.
# Find out if we're using i3 or sway
if ! [[ -n "${WAYLAND_DISPLAY}" ]]; then
cmd="i3-msg"
scratchConfig="${XDG_CONFIG_HOME:-$HOME/.config}/i3"
usingWayland=false
else
cmd="swaymsg"
scratchConfig="${XDG_CONFIG_HOME:-$HOME/.config}/sway"
usingWayland=true
fi
scratchConfig+="/scratchpad"
touch "${scratchConfig}"
# Get the class name of the focused window
if [[ "$usingWayland" == "true" ]]; then
# Wayland/Sway: use swaymsg to get focused window info
class=$($cmd -t get_tree | jq -r '.. | select(.focused? == true) | .app_id // .window_properties.class' | head -n 1)
else
# X11/i3: use xdotool and xprop
if ! command -v xdotool &> /dev/null || ! command -v xprop &> /dev/null; then
notify-send "Error: xdotool and xprop required for i3"
exit 1
fi
windowId=$(xdotool getactivewindow)
class=$(xprop -id "$windowId" WM_CLASS | awk -F '"' '{print $4}')
fi
if [[ -z "$class" ]]; then
notify-send "Unable to move to scratchpad."
exit 1
fi
# Check if it's already in the config
if [[ "$usingWayland" == "true" ]]; then
# Sway uses app_id for Wayland-native apps, class for XWayland apps
if ! grep -q "app_id=\"$class\"" "$scratchConfig" && ! grep -q "class=\"$class\"" "$scratchConfig"; then
echo "for_window [app_id=\"$class\"] move to scratchpad" >> "$scratchConfig"
notify-send "Added window app_id $class to scratchpad"
fi
# Move the window to scratchpad now (try both app_id and class)
$cmd "[app_id=\"$class\"] move to scratchpad" 2>/dev/null || $cmd "[class=\"$class\"] move to scratchpad"
else
# i3 uses class
if ! grep -q "class=\"$class\"" "$scratchConfig"; then
echo "for_window [class=\"$class\"] move to scratchpad" >> "$scratchConfig"
notify-send "Added window class $class to scratchpad"
fi
# Move the window to scratchpad now
$cmd "[class=\"$class\"] move to scratchpad"
fi