Files
PLStatus/get_coords.py
2026-02-17 09:17:46 +03:00

70 lines
2.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Вспомогательный скрипт для подбора координат для config.json.
Запустите его, затем наведите мышь на нужный элемент в окне мессенджера
и нажмите клавишу — в консоль выведется позиция мыши.
Для координат ОТНОСИТЕЛЬНО окна: сначала активируйте окно мессенджера,
запустите скрипт и кликайте по элементам — скрипт покажет и экранные координаты,
и координаты относительно активного окна (если доступно).
"""
import sys
import time
import platform
from pathlib import Path
def get_cursor_pos():
import pyautogui
return pyautogui.position()
def get_active_window_rect():
"""Позиция активного окна (Windows: через pygetwindow, Linux: wmctrl)."""
if platform.system() == "Windows":
try:
import pygetwindow as gw
w = gw.getActiveWindow()
if w:
return (w.left, w.top, w.width, w.height)
except Exception:
pass
return None
try:
import subprocess
out = subprocess.run(["xdotool", "getactivewindow", "getwindowgeometry"], capture_output=True, text=True, timeout=2)
if out.returncode != 0:
return None
# Parse "Position: 100,200 (screen: 0)\nDimensions: 400x300"
pos = dim = None
for line in out.stdout.strip().splitlines():
if line.startswith("Position:"):
part = line.split("(", 1)[0].replace("Position:", "").strip()
a, b = part.split(",")
pos = (int(a.strip()), int(b.strip()))
elif line.startswith("Dimensions:"):
part = line.split(":", 1)[1].strip()
w, h = part.split("x")
dim = (int(w), int(h))
if pos and dim:
return (pos[0], pos[1], dim[0], dim[1])
except Exception:
pass
return None
if __name__ == "__main__":
print("Координаты обновляются в реальном времени. Остановка: Ctrl+C")
try:
while True:
time.sleep(0.2)
x, y = get_cursor_pos()
rect = get_active_window_rect()
if rect:
rel_x = x - rect[0]
rel_y = y - rect[1]
print(f"\rЭкран: ({x}, {y}) | Относительно окна: ({rel_x}, {rel_y}) ", end="", flush=True)
else:
print(f"\rЭкран: ({x}, {y}) ", end="", flush=True)
except KeyboardInterrupt:
print("\nГотово.")