Skip to content

Instantly share code, notes, and snippets.

@stelonix
Last active December 23, 2015 03:29
Show Gist options
  • Save stelonix/6573768 to your computer and use it in GitHub Desktop.
Save stelonix/6573768 to your computer and use it in GitHub Desktop.
Simple Zenity slider to change current window transparency. Needs a better way to find the Window who called the script. Made for pekwm. Requires: transset-df, zenity and xprop. Rev 3: Now honors Zenity's return value
#!/usr/bin/python2
import sys, subprocess, os
from subprocess import PIPE, Popen
from threading import Thread
from Queue import Queue, Empty
def enqueue_output(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
out.close()
#log_file = open(os.path.expanduser("~/opacity_zenity.log"),"a+")
def popen_stdout(args):
p = subprocess.Popen(args, stdout=subprocess.PIPE)
return p.communicate()[0]
def transset_df_set_opacity(opval):
float_val = str(float(opval)/100)
subprocess.call(["transset-df", "-i", targ_win, float_val])
targ_win = str.split(popen_stdout(["xprop","-root","_NET_ACTIVE_WINDOW"])," ")[4].strip(' \t\n\r')
#log_file.write("""Window is "%s".\n"""%targ_win)
cur_opacity = popen_stdout(["xprop","-id",targ_win,"_NET_WM_WINDOW_OPACITY"])
val = str.split(cur_opacity," ")[2]
if val != "not":
cur_op = str(int(100*float(str.split(cur_opacity," ")[2].strip(' \t\n\r'))/0xFFFFFFFF))
else:
cur_op = "100"
#log_file.write("""Current opacity is "%s".\n"""%cur_op)
p = Popen(["zenity","--scale","--print-partial", "--value=%s"%cur_op], stdout=PIPE, bufsize=1, close_fds=True)
q = Queue()
t = Thread(target=enqueue_output, args=(p.stdout, q))
t.daemon = True
t.start()
#log_file.write("Zenity running")
while True:
try: line = q.get_nowait()
except Empty:
if not t.is_alive():
p.wait()
if p.returncode == 1:
transset_df_set_opacity(cur_op)
break
else:
transset_df_set_opacity(line)
#log_file.write("Opacity set to %s\n"%float_val)
#log_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment