Created
February 17, 2026 10:38
-
-
Save icedmoca/403f7882cd4060ac247703f9b7ba0c7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -e | |
| KWINRULES=~/.config/kwinrulesrc | |
| echo "Focus the window now. Capturing in 3 seconds..." | |
| sleep 3 | |
| RAW=$(gdbus call --session --dest org.kde.KWin --object-path /KWin --method org.kde.KWin.queryWindowInfo 2>/dev/null) || { | |
| echo "Error: could not get window info from KWin. Is the window focused?" | |
| exit 1 | |
| } | |
| parse_num() { echo "$RAW" | sed -n "s/.*'$1': <\([0-9.]*\).*/\1/p" | head -1; } | |
| WMCLASS=$(echo "$RAW" | sed -n "s/.*'resourceClass': <'\([^']*\)'.*/\1/p" | head -1) | |
| POS_X=$(parse_num x | cut -d. -f1) | |
| POS_Y=$(parse_num y | cut -d. -f1) | |
| WIDTH=$(parse_num width | cut -d. -f1) | |
| HEIGHT=$(parse_num height | cut -d. -f1) | |
| [[ -z "$POS_X" ]] && POS_X=0 | |
| [[ -z "$POS_Y" ]] && POS_Y=0 | |
| [[ -z "$WIDTH" || -z "$HEIGHT" ]] && { echo "Could not parse geometry. KWin output was:"; echo "$RAW"; exit 1; } | |
| [[ -z "$WMCLASS" ]] && WMCLASS="google-chrome" | |
| POS="${POS_X},${POS_Y}" | |
| SIZE="${WIDTH},${HEIGHT}" | |
| echo "Detected window class: $WMCLASS" | |
| echo "Position: $POS Size: $SIZE" | |
| UUID=$(uuidgen 2>/dev/null | tr -d '-' || echo "$(cat /proc/sys/kernel/random/uuid 2>/dev/null | tr -d '-' || echo "chrome-$(date +%s)")") | |
| RULE="[${UUID}] | |
| Description=Window position and size (forced) | |
| position=$POS | |
| positionrule=4 | |
| size=$SIZE | |
| sizerule=4 | |
| types=1 | |
| wmclass=$WMCLASS | |
| wmclassmatch=1 | |
| wmclasscomplete=true | |
| " | |
| cp "$KWINRULES" "${KWINRULES}.bak" | |
| current_rules=$(awk '/^rules=/ { print $0; exit }' "$KWINRULES") | |
| new_rules="rules=${UUID},${current_rules#rules=}" | |
| sed -i "s/^rules=.*/$new_rules/" "$KWINRULES" | |
| old_count=$(grep '^count=' "$KWINRULES" | head -1) | |
| new_count=$(( ${old_count#count=} + 1 )) | |
| sed -i "s/^count=.*/count=$new_count/" "$KWINRULES" | |
| printf '%s' "$RULE" > /tmp/kwinrule.txt | |
| python3 - "$KWINRULES" /tmp/kwinrule.txt << 'PY' | |
| import sys | |
| path = sys.argv[1] | |
| rule_path = sys.argv[2] | |
| p = open(path).read() | |
| rule = open(rule_path).read() | |
| marker = "[General]" | |
| if marker not in p: | |
| raise SystemExit("Could not find [General] in kwinrulesrc") | |
| idx = p.index(marker) | |
| new_content = p[:idx] + rule + p[idx:] | |
| open(path, "w").write(new_content) | |
| PY | |
| rm -f /tmp/kwinrule.txt | |
| echo "Done. Rule added for $WMCLASS: position $POS, size $SIZE (forced)." | |
| echo "Close the window and open it again for the rule to apply. New windows will open at that position and size." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment