Skip to content

Instantly share code, notes, and snippets.

@EleotleCram
Last active May 21, 2025 11:47
Show Gist options
  • Save EleotleCram/3a72c5896bdefa738d8aa41c8f8520b5 to your computer and use it in GitHub Desktop.
Save EleotleCram/3a72c5896bdefa738d8aa41c8f8520b5 to your computer and use it in GitHub Desktop.
autocrat.sh: devilspie(2) written in bash, but simpler and more versatile
#!/bin/bash
# autocrat.sh: devilspie(2) written in bash, but simpler and more versatile
# Decrees can be written by asking ChatGPT to formulate them.
# Use the following priming prompt to do so:
cat > /dev/null <<EOF
I am building a Bash script called autocrat.sh that manages window behavior in Linux, similar to devilspie2.
Each rule is a Bash function named decree_<something>.
Please write a decree that does [describe behavior], using tools like wmctrl or xdotool where needed.
The function should be idempotent and concise.
EOF
# Decrees — define as many as you like
# Rationale: "TurboVNC: " requires a lot of title real-estate in a taskbar, this decree mitigates that.
decree_turbovnc_windows_shall_be_so_named() {
# Get list of all windows with TurboVNC in the title
wmctrl -l | grep "TurboVNC: " | while read -r win_id _ _ title; do
# Strip "TurboVNC: " from the title
new_title="${title#TurboVNC: }"
wmctrl -i -r "$win_id" -T "$new_title"
done
}
# Main enforcement function
enforce_decrees() {
for decree in $(declare -F | awk '{print $3}' | grep '^decree_'); do
"$decree"
done
}
SELF_SCRIPT="$(realpath "$0")"
LAST_MOD_TIME=$(stat -c %Y "$SELF_SCRIPT")
# Interval in seconds between enforcement cycles
INTERVAL=2
# Main loop
while true; do
enforce_decrees
# Check if the script has changed
NEW_MOD_TIME=$(stat -c %Y "$SELF_SCRIPT")
if [[ "$NEW_MOD_TIME" != "$LAST_MOD_TIME" ]]; then
echo "[autocrat] Detected script update. Restarting..."
exec "$SELF_SCRIPT" "$@"
fi
sleep "$INTERVAL"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment