Repository Public
1// Sends a left click to one process at a point given relative to the top-left corner of its front2// window, without moving the pointer or activating the app (evidence for hit-testing checks).3// Usage: swift Scripts/click.swift <pid> <x> <y> [right]4import CoreGraphics5import Foundation6let args = CommandLine.arguments7guard args.count >= 4, let pid = Int32(args[1]), let x = Double(args[2]), let y = Double(args[3]) else { print("usage: click <pid> <x> <y> [right]"); exit(2) }8let list = CGWindowListCopyWindowInfo([.optionOnScreenOnly, .excludeDesktopElements], kCGNullWindowID) as? [[String: Any]] ?? []9guard let win = list.first(where: { ($0["kCGWindowOwnerPID"] as? Int32) == pid && ($0["kCGWindowLayer"] as? Int) == 0 }),10 let b = win["kCGWindowBounds"] as? [String: Double] else { print("no window"); exit(1) }11let point = CGPoint(x: (b["X"] ?? 0) + x, y: (b["Y"] ?? 0) + y)12let right = args.count > 413for type in right ? [CGEventType.rightMouseDown, .rightMouseUp] : [CGEventType.mouseMoved, .leftMouseDown, .leftMouseUp] {14 let event = CGEvent(mouseEventSource: nil, mouseType: type, mouseCursorPosition: point, mouseButton: right ? .right : .left)15 event?.postToPid(pid)16 usleep(60_000)17}18print("clicked \(point)")19