Skip to content

Instantly share code, notes, and snippets.

@jaburns
Created February 6, 2021 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaburns/1f88f78ff3c21e42c82ba6f165839b1a to your computer and use it in GitHub Desktop.
Save jaburns/1f88f78ff3c21e42c82ba6f165839b1a to your computer and use it in GitHub Desktop.
Spawn a window with an image in it and follow vscode around
#!/usr/bin/env python3
import os
import subprocess
import time
import re
code_windows = subprocess.check_output("wmctrl -lx | grep 'code.Code' | cut -f1 -d' '", shell=True).decode('utf-8').strip().split('\n')
for i in range(len(code_windows)):
subprocess.Popen([ "display", '-crop', '10,10,100,100', PAPER_PATH ])
time.sleep(5)
image_windows = subprocess.check_output("wmctrl -lx | grep 'ImageMagick:' | cut -f1 -d' '", shell=True).decode('utf-8').strip().split('\n')
if len(code_windows) != len(image_windows):
print("didnt open windows or didnt wait enough")
exit(1)
def get_win_dims_str(win_id):
info = subprocess.check_output("xwininfo -id "+win_id, shell=True).decode('utf-8').strip().split('\n')
win = []
for i in range(6):
win.append(int(re.sub(r'.*:', '', info[2+i])))
return str(win[0] - win[2]) + ',' + str(win[1] - win[3]) + ',' + str(win[4]) + ',' + str(win[5])
def get_active_win_id():
proc_output = subprocess.check_output("xprop -root _NET_ACTIVE_WINDOW", shell=True).decode('utf-8').strip()
return int(re.sub(r',.*', '', re.sub(r'.*#', '', proc_output)), 16)
old_dims = ['' for _ in code_windows]
was_active = 0
while True:
now_active = get_active_win_id()
for i in range(len(code_windows)):
dims = get_win_dims_str(code_windows[i])
if dims != old_dims[i] or (int(code_windows[i],16) == now_active and now_active != was_active):
os.system('wmctrl -ir ' + image_windows[i] + ' -e "0,' + dims + '"')
os.system('wmctrl -ia ' + image_windows[i])
os.system('wmctrl -ia ' + code_windows[i])
time.sleep(.5)
old_dims[i] = dims
was_active = now_active
time.sleep(.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment