-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch.py
More file actions
executable file
·27 lines (21 loc) · 1.08 KB
/
switch.py
File metadata and controls
executable file
·27 lines (21 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/python3
import argparse
import subprocess
def syscall(*args):
""" Helper method to make a syscall, check for errors, and return output as a string."""
return subprocess.run(args, capture_output=True, check=True, text=True).stdout
# Parse args (accept exactly one of prev or next)
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-p", "--prev", help="switch to the previous window", action="store_true")
group.add_argument("-n", "--next", help="switch to the next window", action="store_true")
args = parser.parse_args()
# Get list of window handles from wmctrl and convert to decimal
windows_res = syscall("wmctrl", "-l")
windows = [str(int(s.partition(" ")[0], 16)) for s in windows_res.splitlines()]
# Get the current window handle
current_window = syscall("xdotool", "getwindowfocus").strip()
# Calculate the window handle to switch to and switch to it
step = 1 if args.next else -1
switch_to_window = windows[(windows.index(current_window) + step) % len(windows)]
syscall("xdotool", "windowactivate", switch_to_window)