#!/usr/bin/env python3 """Klickt die Aktionsknöpfe des Tisches an - Positionen aus dem Fensterrahmen. Die Knöpfe liegen immer an derselben Stelle im Fenster, deshalb werden die Positionen als Anteil der Fenstergröße berechnet. Verschieben und Größe ändern macht also nichts. python3 click.py --where # Positionen anzeigen python3 click.py fold # Fold klicken python3 click.py call # Call klicken python3 click.py raise # Raise klicken python3 click.py size 33 # Einsatz-Voreinstellung 33% wählen """ import os as _os, sys as _sys _w = _os.path.dirname(_os.path.abspath(__file__)) while _w != "/" and not _os.path.isdir(_os.path.join(_w, "hand")): _w = _os.path.dirname(_w) WURZEL = _w for _u in ("lesen", "entscheiden", "hand", "lobby", "korpus", "betrieb"): _q = _os.path.join(_w, _u) if _os.path.isdir(_q) and _q not in _sys.path: _sys.path.insert(0, _q) import ctypes import random import subprocess import sys import time as _time import maus FW = "/System/Library/Frameworks/ApplicationServices.framework/ApplicationServices" AS = ctypes.cdll.LoadLibrary(FW) class CGPoint(ctypes.Structure): _fields_ = [("x", ctypes.c_double), ("y", ctypes.c_double)] AS.CGEventCreateMouseEvent.restype = ctypes.c_void_p AS.CGEventCreateMouseEvent.argtypes = [ctypes.c_void_p, ctypes.c_uint32, CGPoint, ctypes.c_uint32] AS.CGEventPost.argtypes = [ctypes.c_uint32, ctypes.c_void_p] AS.CGEventPostToPid.argtypes = [ctypes.c_int, ctypes.c_void_p] AS.CGEventCreate.restype = ctypes.c_void_p AS.CGEventCreate.argtypes = [ctypes.c_void_p] AS.CGEventGetLocation.restype = CGPoint AS.CGEventGetLocation.argtypes = [ctypes.c_void_p] MOVED, DOWN, UP = 5, 1, 2 # Anteil der Fensterbreite/-höhe (aus einem Bild vom Tisch abgelesen) SPOTS = { "fold": (0.659, 0.943), "call": (0.796, 0.943), "raise": (0.933, 0.943), "size33": (0.616, 0.858), "size50": (0.677, 0.858), "size75": (0.735, 0.858), "max": (0.784, 0.858), "amount": (0.873, 0.855), } def osa(script): r = subprocess.run(["osascript", "-e", script], capture_output=True, text=True) return (r.stdout or r.stderr).strip() def window_rect(): pids = subprocess.run(["pgrep", "-f", "CoinPoker Game"], capture_output=True, text=True).stdout.split() if not pids: return None proc = f'(first process whose unix id is {pids[0]})' pos = osa(f'tell application "System Events" to tell {proc} to get position of window 1') size = osa(f'tell application "System Events" to tell {proc} to get size of window 1') try: x, y = [float(v) for v in pos.replace(" ", "").split(",")[:2]] w, h = [float(v) for v in size.replace(" ", "").split(",")[:2]] except Exception: return None return x, y, w, h def point_for(name, rect): if name not in SPOTS: return None x, y, w, h = rect fx, fy = SPOTS[name] return round(x + w * fx), round(y + h * fy) def nach_vorn(fenster): """Fenster anheben und die App nach vorn holen. Ein Klick an den Prozess (CGEventPostToPid) erreicht den Unity-Tisch, einen Chrome-Klon aber gar nicht; ein globaler Klick erreicht beide. Ein globaler trifft nur, was oben liegt - deshalb muss das Zielfenster vorher nach vorn. """ if not fenster: return pid = fenster.get("pid") titel = (fenster.get("titel") or "").replace('"', '\\"') proc = f'(first process whose unix id is {pid})' if titel: osa(f'tell application "System Events" to perform action "AXRaise" of ' f'(first window whose name is "{titel}") of {proc}') osa(f'tell application "System Events" to set frontmost of {proc} to true') def zeiger(): """Wo der Mauszeiger gerade steht.""" p = AS.CGEventGetLocation(AS.CGEventCreate(None)) return p.x, p.y def _senden(art, x, y): AS.CGEventPost(0, AS.CGEventCreateMouseEvent(None, art, CGPoint(x, y), 0)) def click(px, py, pid=None, fenster=None, halb=(0.0, 0.0), rng=random): """Klickt global, mit der Hand geführt (hand/maus.py): vom aktuellen Zeiger auf einem gekrümmten Weg zu einem gestreuten Punkt auf dem Knopf (Mitte px, py; halbe Größe `halb`), kurz verweilen, drücken, halten, loslassen. Mit `fenster` (aus hand/fenster.py) wird das Fenster vorher angehoben.""" if fenster: pid = fenster.get("pid") nach_vorn(fenster) weg, verweilen, halten = maus.plan(zeiger(), (px, py), halb, rng) beginn = _time.monotonic() for ms, x, y in weg: warte = beginn + ms / 1000.0 - _time.monotonic() if warte > 0: _time.sleep(warte) _senden(MOVED, x, y) x, y = weg[-1][1:] _time.sleep(verweilen / 1000.0) _senden(DOWN, x, y) _time.sleep(halten / 1000.0) _senden(UP, x, y) def main(): args = sys.argv[1:] rect = window_rect() if not rect: print("kein Tischfenster") return 1 x, y, w, h = rect print(f"Tischfenster: {int(x)},{int(y)} {int(w)}x{int(h)}") if not args or args[0] == "--where": for name in SPOTS: px, py = point_for(name, rect) print(f" {name:<7} -> {px},{py}") return 0 key = args[0] if key == "size": key = "size" + args[1] px, py = point_for(key, rect) if px is None: print(f"unbekannt: {key}; bekannt: {', '.join(SPOTS)}") return 1 click(px, py, halb=maus.halbe_groesse(key, rect)) print(f"Klick auf {key} bei {px},{py}") return 0 if __name__ == "__main__": sys.exit(main())