Skip to content

Instantly share code, notes, and snippets.

@nirbheek
Last active March 6, 2024 09:41
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save nirbheek/5589105 to your computer and use it in GitHub Desktop.
Save nirbheek/5589105 to your computer and use it in GitHub Desktop.
A tiny script to set the transparency for X windows. If AUTOMAGIC_MODE is "true", it tries to find all gnome-terminal windows and applies the transparency to all of them.
#!/bin/bash
# vim: set sts=4 sw=4 et tw=0 :
#
# License: BSD
AUTOMAGIC_MODE="true"
OPACITY_100="0xffffffff"
OPACITY_0="0x0"
: ${XWININFO:=$(type -P xwininfo)}
[[ -z ${XWININFO} ]] && { echo "You need to install xwininfo"; exit 1; }
: ${XPROP:=$(type -P xprop)}
[[ -z ${XPROP} ]] && { echo "You need to install xprop"; exit 1; }
TRANSPARENCY_PERCENT=$1
[[ -z "${TRANSPARENCY_PERCENT}" ]] && { echo "Usage: $0 <transparency in percentage>"; exit 1; }
if [[ ${AUTOMAGIC_MODE} != true ]]; then
echo "Click on a terminal window to set its transparency as specified (${TRANSPARENCY_PERCENT}%)"
TERMINAL_WINDOW_XID=$("$XWININFO" | sed -n 's/.*Window id: \([0-9a-fx]\+\).*/\1/p')
else
# This is very fragile
TERMINAL_WINDOW_XID=$("$XWININFO" -root -tree | grep -v "Terminal" | sed -n 's/^[[:space:]]\+\([0-9a-fx]\+\).*gnome-terminal.*/\1/p')
fi
if [[ $TRANSPARENCY_PERCENT = 100 ]]; then
TRANSPARENCY_HEX="$OPACITY_100"
elif [[ $TRANSPARENCY_PERCENT = 0 ]]; then
TRANSPARENCY_HEX="$OPACITY_0"
else
TRANSPARENCY_HEX=$(printf "0x%x" $((($(printf "%d" $OPACITY_100) * $TRANSPARENCY_PERCENT)/100)))
fi
echo "Setting transparency to $TRANSPARENCY_HEX (${TRANSPARENCY_PERCENT}%)"
for each in $TERMINAL_WINDOW_XID; do
"$XPROP" -id $each -f _NET_WM_WINDOW_OPACITY 32c -set _NET_WM_WINDOW_OPACITY $TRANSPARENCY_HEX
done
@MikyStar
Copy link

Hi !

This is awesome but I'm trying to apply it automatically everytime I launch a new gnome-terminal instance.
I've tried putting sh ~/Documents/yourScript.sh in my .zshrc but this way doesn't seems to work (automagic is enabled of course). The transparency will not be applied to the current terminal but to all the other instances.

Do you have a solution ?

@elig0n
Copy link

elig0n commented Mar 31, 2019

Doesn't work for me with

# GNOME Terminal 3.30.2 using VTE 0.54.2 +GNUTLS
gnome-terminal-fedora on Manjaro

@avinashkanaujiya
Copy link

change type -P xprop to type xprop & change type -P xwininfo to type xwininfo.

And it does not work.

@LeisureLinux
Copy link

LeisureLinux commented Oct 5, 2022

Pity that this is not as a repo. so we can contribute.
So far, found not working well on byobu, two display scenario. my fix:
only grep those begin with first 5 space, not other like 7 or whatever so don't care the child windows. Also byobu will work as well.
TERMINAL_WINDOW_XID=$("$XWININFO" -root -tree | grep -E -v "Terminal|Gnome-terminal-server" | sed -n 's/^[[:space:]]{5}([0-9a-fx]+).gnome-terminal./\1/p')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment