Skip to content

Instantly share code, notes, and snippets.

@sagaban
Forked from ikbelkirasan/change_window_opacity.sh
Created November 20, 2022 22:56
Show Gist options
  • Save sagaban/13cd7a6530640ae9a698b34232612dd5 to your computer and use it in GitHub Desktop.
Save sagaban/13cd7a6530640ae9a698b34232612dd5 to your computer and use it in GitHub Desktop.
Change window opacity in Linux using keyboard shortcuts

Change active window opacity in Linux using keyboard shortcut

Prerequisites

You need these dependencies on your machine:

  • xprop
  • xdotool

Setup

  1. Put the script somewhere e.g. ~/.scripts/change_window_opacity.sh

make it executable chmod +x ~/.scripts/change_window_opacity.sh

  1. Open Settings center -> Keyboard

  2. Create a custom shortcut for increasing the window opacity and set the hotkey to whatever you want, then set the command to be: bash -c "~/.scripts/change_window_opacity.sh -i".

  3. Same for decreasing the window opacity, set the command to be: bash -c "~/.scripts/change_window_opacity.sh -d".

Done. Have fun! :)

#!/bin/bash
function get_active_window() {
printf "0x%08x" $(xdotool getactivewindow)
}
function get_current_opacity() {
window="$1"
opacity=$(xprop -id $window | grep _NET_WM_WINDOW_OPACITY | awk '{print $3}')
if [ -z $opacity ]; then
opacity=0xffffffff
fi
echo $opacity
}
function set_opacity() {
window=$1
opacity=$2
if (( opacity <= 0 )); then
opacity=0
elif (( opacity >= 0xffffffff )); then
opacity=0xffffffff
fi
xprop -id $window -f _NET_WM_WINDOW_OPACITY 32c -set _NET_WM_WINDOW_OPACITY $opacity
}
function increase_opacity() {
window=$(get_active_window)
opacity=$(get_current_opacity $window)
percentage=$((opacity*100/0xffffffff))
new_opacity=$((percentage+10))
new_opacity=$((new_opacity*0xffffffff/100))
set_opacity $window $new_opacity
}
function decrease_opacity() {
window=$(get_active_window)
opacity=$(get_current_opacity $window)
percentage=$((opacity*100/0xffffffff))
new_opacity=$((percentage-10))
new_opacity=$((new_opacity*0xffffffff/100))
set_opacity $window $new_opacity
}
function parse_args() {
arg="$1"
if [ -z "$arg" ]; then
echo "Usage: $0 [ -i | -d ]"
fi
if [ "$arg" == '-i' ]; then
increase_opacity
elif [ "$arg" == '-d' ]; then
decrease_opacity
fi
}
parse_args $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment