Ariver
2026-06-29 cea8745cfadb698774cb7b4f930c6f10b7cbdf3c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import CoreGraphics
import Foundation
 
func usage() -> Never {
    FileHandle.standardError.write(
        Data("usage: round1-visible-owner-window-sampler.swift <owner-name> <duration-seconds> <interval-seconds> <output-path>\n".utf8)
    )
    exit(2)
}
 
let args = CommandLine.arguments
guard args.count == 5,
      let duration = Double(args[2]),
      let interval = Double(args[3]),
      duration > 0,
      interval > 0 else {
    usage()
}
 
let ownerName = args[1]
let outputPath = args[4]
let deadline = Date().addingTimeInterval(duration)
var sampleCount = 0
 
func describe(_ item: [String: Any]) -> String {
    let title = item[kCGWindowName as String] as? String ?? ""
    let layer = item[kCGWindowLayer as String] as? Int ?? 0
    let number = item[kCGWindowNumber as String] as? Int ?? 0
    let bounds = item[kCGWindowBounds as String] as? [String: Any] ?? [:]
    return "windowNumber=\(number) title=\"\(title)\" layer=\(layer) bounds=\(bounds)"
}
 
func writeReport(_ text: String) {
    do {
        try text.write(toFile: outputPath, atomically: true, encoding: .utf8)
    } catch {
        FileHandle.standardError.write(Data("failed to write sampler report: \(error)\n".utf8))
    }
}
 
repeat {
    sampleCount += 1
    let windows = CGWindowListCopyWindowInfo([.optionOnScreenOnly, .excludeDesktopElements], kCGNullWindowID) as? [[String: Any]] ?? []
    let matches = windows
        .filter { item in
            (item[kCGWindowOwnerName as String] as? String) == ownerName
        }
        .map(describe)
 
    if !matches.isEmpty {
        let report = """
        owner=\(ownerName)
        samples=\(sampleCount)
        visibleWindows=\(matches.count)
        \(matches.joined(separator: "\n"))
        """
        writeReport(report + "\n")
        print(report)
        exit(1)
    }
 
    if Date() < deadline {
        Thread.sleep(forTimeInterval: interval)
    }
} while Date() < deadline
 
let report = "owner=\(ownerName) samples=\(sampleCount) visibleWindows=0 duration=\(duration) interval=\(interval)"
writeReport(report + "\n")
print(report)