Skip to content

Instantly share code, notes, and snippets.

@jkfran
Last active November 16, 2017 21:16
Show Gist options
  • Save jkfran/8b7d0b915adf5d9b836d35964f24e486 to your computer and use it in GitHub Desktop.
Save jkfran/8b7d0b915adf5d9b836d35964f24e486 to your computer and use it in GitHub Desktop.
This is a simple script to move between windows sorted by creation. Useful for keyboard shorcuts. I personally use it to move through the windows on xfce-panel
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""This is a simple script to move between windows sorted by creation.
Useful for keyboard shorcuts. I personally use it to move through the
windows on xfce-panel. Usage: ./controlwindows.py up/down
Dependecies: wmctrl, xdotool and xprop"""
import subprocess
import sys
def get_value(command):
return subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8").strip()
def run_command(command):
subprocess.Popen(["/bin/bash", "-c", command])
current_workspace = [l for l in get_value("wmctrl -d").splitlines() if l.split(" ")[2] == "*"][0][0]
current_window = int(get_value("xdotool getwindowfocus"))
position = 0
windows = []
for window in get_value("wmctrl -l").splitlines():
window = window.split()
if window[1] != current_workspace:
continue
# window_info = get_value("xprop -id " + window[0])
# if ("_TYPE_NORMAL" in window_info, "TYPE_DIALOG" in window_info).count(True) != 1:
# continue
if int(window[0], 16) == current_window:
position = len(windows)
windows.append(window)
try:
if sys.argv[1] == "up" and position > 0:
position -= 1
elif sys.argv[1] == "down" and position + 1 < len(windows):
position += 1
next_window = windows[position][0]
run_command("wmctrl -ia " + next_window)
except IndexError:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment