From ea6bf48d3db8a77f5958765b98124db11226a409 Mon Sep 17 00:00:00 2001 From: Wis <~@wis.am> Date: Fri, 12 Apr 2019 05:20:46 +0300 Subject: [PATCH] add windows_with_name_in_workspace.py --- examples/windows_with_name_in_workspace.py | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 examples/windows_with_name_in_workspace.py diff --git a/examples/windows_with_name_in_workspace.py b/examples/windows_with_name_in_workspace.py new file mode 100644 index 0000000..119a1a3 --- /dev/null +++ b/examples/windows_with_name_in_workspace.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +# print Ids of windows with WM_CLASS on current workspace or workspace with given name +from subprocess import check_output, STDOUT +import i3ipc +import re + +def get_windows_on_current_ws(conn): + return filter(lambda x: x.window, + conn.get_tree().find_focused().workspace().descendents()) + +def workspace_by_name(conn, workspace): + return next(filter(lambda ws: ws.name==workspace, conn.get_tree().workspaces()), None) + +def get_windows_on_ws(conn, name): + return filter(lambda x: x.window, + workspace_by_name(conn, name).descendents()) + +def main(args): + if not args.name: + print("a (non-empty) name argument is required") + return + conn = i3ipc.Connection() + windows = get_windows_on_current_ws(conn) + if args.workspace: + windows = get_windows_on_ws(conn, args.workspace) + for w in windows: + name = check_output( + "xprop -id " + str(w.window) + " 2> /dev/null | awk '/^WM_CLASS/{print $4}'", + shell=True, + encoding="utf8") + name = re.sub(r'[\n"]', "", name) + if name == args.name: + print(w.window) + +if __name__ == '__main__': + + from argparse import ArgumentParser + + parser = ArgumentParser(prog='windows_with_name_in_workspace.py', + description="Program using i3ipc to select the nth window from a workspace.") + + parser.add_argument('--name', "--string", type=str, default=None, help="""windows name in current workspace""") + parser.add_argument('--workspace', default=None, help="""workspace name""") + + main(parser.parse_args())