import CoreGraphics import Foundation // Sucht das größte sichtbare Fenster eines Programms und gibt seine Kennung // aus. Damit lässt sich `screencapture -l` aufrufen — das fotografiert // genau dieses eine Fenster und sonst nichts vom Bildschirm. // // Gebaut, weil es sonst keinen Weg dorthin gibt: das System-Python bringt // PyObjC nicht mehr mit, und `screencapture` kennt nur Kennungen, keine Namen. let name = CommandLine.arguments.dropFirst().first ?? "Flixy" guard let windows = CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID) as? [[String: Any]] else { FileHandle.standardError.write(Data("Fensterliste nicht lesbar\n".utf8)) exit(2) } let candidates = windows.filter { window in guard let owner = window[kCGWindowOwnerName as String] as? String, owner == name else { return false } // Ebene 0 ist das normale Fenster; alles darüber sind Menüs und Schatten. guard (window[kCGWindowLayer as String] as? Int) == 0 else { return false } return true } let largest = candidates.max { a, b in func area(_ w: [String: Any]) -> Double { guard let bounds = w[kCGWindowBounds as String] as? [String: Any], let width = bounds["Width"] as? Double, let height = bounds["Height"] as? Double else { return 0 } return width * height } return area(a) < area(b) } guard let window = largest, let id = window[kCGWindowNumber as String] as? Int else { FileHandle.standardError.write(Data("Kein sichtbares Fenster von „\(name)“\n".utf8)) exit(1) } if let bounds = window[kCGWindowBounds as String] as? [String: Any], let width = bounds["Width"] as? Double, let height = bounds["Height"] as? Double { FileHandle.standardError.write(Data("\(name): \(Int(width))×\(Int(height))\n".utf8)) } print(id)