Skip to content

Instantly share code, notes, and snippets.

@fuzunspm
Created May 25, 2025 18:06
Show Gist options
  • Save fuzunspm/0b70d3b2de9e9842302b3148e893b979 to your computer and use it in GitHub Desktop.
Save fuzunspm/0b70d3b2de9e9842302b3148e893b979 to your computer and use it in GitHub Desktop.
Sway window opacity change on keybindings

Sway opacity changer

  • This script basically increase/decrease focused window opacity on keybind.
  • It stores window opacity values in a temporary file since sway is not reliable (have no idea)
bindsym $mod+KP_Add exec /path/to/script/opacity.sh up
bindsym $mod+KP_Subtract exec /path/to/script/opacity.sh down
#!/bin/bash
direction="$1"
step=0.05
min=0.1
max=1.0
cache_file="/tmp/sway_opacity_cache"
window_id=$(swaymsg -t get_tree | jq '.. | objects | select(.focused == true) | .id')
[ -z "$window_id" ] && exit 1
current_opacity=$(grep "^$window_id " "$cache_file" 2>/dev/null | awk '{print $2}')
[ -z "$current_opacity" ] && current_opacity=1.0
if [ "$direction" == "up" ]; then
new_opacity=$(echo "$current_opacity + $step" | bc)
else
new_opacity=$(echo "$current_opacity - $step" | bc)
fi
new_opacity=$(echo "$new_opacity" | awk -v min="$min" -v max="$max" '
{ if ($1 < min) print min; else if ($1 > max) print max; else print $1 }')
swaymsg "[con_id=$window_id]" opacity "$new_opacity"
grep -v "^$window_id " "$cache_file" 2>/dev/null > "${cache_file}.tmp"
echo "$window_id $new_opacity" >> "${cache_file}.tmp"
mv "${cache_file}.tmp" "$cache_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment