#!/usr/bin/env python3 import json import sys from gi.repository import Atspi mode, limit = sys.argv[1], int(sys.argv[2]) Atspi.init() desktop = Atspi.get_desktop(0) nodes = [] def walk(node, depth=0): if len(nodes) >= limit or node is None or depth > 30: return try: role = node.get_role_name() or '' name = node.get_name() or '' component = node.get_component() rect = component.get_extents(Atspi.CoordType.SCREEN) if component else None if role and (name or rect): nodes.append({'role': role, 'name': name, 'rect': [rect.x, rect.y, rect.width, rect.height] if rect else None, 'state': [str(s) for s in node.get_state_set().get_states()]}) for i in range(node.get_child_count()): walk(node.get_child_at_index(i), depth + 1) except Exception: return if mode == 'focused': for i in range(desktop.get_child_count()): app = desktop.get_child_at_index(i) try: for j in range(app.get_child_count()): window = app.get_child_at_index(j) if window.get_state_set().contains(Atspi.StateType.ACTIVE): walk(window) except Exception: continue else: walk(desktop) print(json.dumps(nodes, ensure_ascii=False))