Created
March 6, 2022 23:22
-
-
Save titusfx/b968357d6baeb17acdbec457a3648f09 to your computer and use it in GitHub Desktop.
Get windows dimension and location in python with xdotool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You need to install | |
# sudo apt-get install -y xdotool | |
import subprocess | |
import re | |
def clean_output(output_b): | |
return output_b.decode("utf-8").strip().split("\n") | |
def getWindowIdGeometry(id): | |
output_b, error_b = subprocess.Popen( | |
["xdotool", "getwindowgeometry", str(id)], | |
stdout = subprocess.PIPE, stderr= subprocess.PIPE | |
).communicate() | |
error = clean_output(error_b) | |
windows_id, position_screen, size = clean_output(output_b) | |
position_template = r'Position: (?P<x>-?\d+),(?P<y>-?\d+) \(screen: (?P<screen_number>\d+)\)' | |
size_template = r'Geometry: (?P<width>\d+)x(?P<height>\d+)' | |
g = re.match(position_template , position_screen.strip()) | |
result = {"id":id} | |
result["window_x"] = int(g.group('x')) | |
result["window_y"] = int(g.group('y')) | |
result["window_screen_number"] = int(g.group('screen_number')) | |
g_wh = re.match(size_template , size.strip()) | |
result["width"] = int(g_wh.group('width')) | |
result["height"] = int(g_wh.group('height')) | |
return result | |
def getAllWindowsIds(title_contains = 'Poker'): | |
output_b, error_b = subprocess.Popen( | |
["xdotool", "search", "--name", title_contains], | |
stdout = subprocess.PIPE, stderr= subprocess.PIPE).communicate() | |
ids_window = clean_output(output_b) | |
error = clean_output(error_b) | |
return ids_window | |
def getAllWindowsPositions(title_contains = 'Poker'): | |
ids = getAllWindowsIds(title_contains) | |
result = [] | |
for id in ids: | |
info = getWindowIdGeometry(id) | |
result.append(info) | |
return result | |
# print(getAllWindowsPositions('Chrome')) | |
# print(getAllWindowsPositions()) | |
# ask for user input on terminal | |
title = input("Type title window substring (insensitive case): ") | |
print(getAllWindowsPositions(title)) | |
# Output | |
#[{'id': '56682625', 'window_x': 2534, 'window_y': -23, 'window_screen_number': 0, 'width': 976, 'height': 1132}, {'id': '65011713', 'window_x': 2633, 'window_y': 75, 'window_screen_number': 0, 'width': 1848, 'height': 1043}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment