Ariver
2026-06-04 247c82b195be7cb7311ba6c5b9aca358fb91e0dc
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
import AppKit
import CoreGraphics
 
// Find visible windows across all apps
let apps = NSWorkspace.shared.runningApplications
 
// specifically look for Terminal
let terminalApps = apps.filter { app in
    guard let name = app.localizedName else { return false }
    return name.lowercased().contains("terminal")
}
 
print("Terminal-like apps found: \(terminalApps.count)")
for app in terminalApps {
    print("  \(app.localizedName ?? "?") PID=\(app.processIdentifier)")
    
    let axApp = AXUIElementCreateApplication(app.processIdentifier)
    var windowsRef: CFTypeRef?
    let result = AXUIElementCopyAttributeValue(axApp, kAXWindowsAttribute as CFString, &windowsRef)
    
    if result == .success, let windows = windowsRef as? [AXUIElement] {
        print("  Windows: \(windows.count)")
        for (i, w) in windows.enumerated() {
            var pos: CFTypeRef?, size: CFTypeRef?, title: CFTypeRef?
            
            let titleResult = AXUIElementCopyAttributeValue(w, kAXTitleAttribute as CFString, &title)
            let titleStr = (title as? String) ?? "(no title)"
            
            var posVal = CGPoint.zero, sizeVal = CGSize.zero
            let posOK = AXUIElementCopyAttributeValue(w, kAXPositionAttribute as CFString, &pos) == .success &&
                        AXValueGetValue(pos as! AXValue, .cgPoint, &posVal)
            let sizeOK = AXUIElementCopyAttributeValue(w, kAXSizeAttribute as CFString, &size) == .success &&
                         AXValueGetValue(size as! AXValue, .cgSize, &sizeVal)
            
            if posOK && sizeOK {
                print("  [\(i)] \"\(titleStr)\" at (\(posVal.x), \(posVal.y)) size (\(sizeVal.width), \(sizeVal.height))")
            }
            
            // Min/Max
            var minVal: CFTypeRef?, maxVal: CFTypeRef?
            if AXUIElementCopyAttributeValue(w, "AXMinSize" as CFString, &minVal) == .success {
                var minS = CGSize.zero
                if AXValueGetValue(minVal as! AXValue, .cgSize, &minS) {
                    print("       MinSize: (\(minS.width), \(minS.height))")
                }
            }
            if AXUIElementCopyAttributeValue(w, "AXMaxSize" as CFString, &maxVal) == .success {
                var maxS = CGSize.zero
                if AXValueGetValue(maxVal as! AXValue, .cgSize, &maxS) {
                    print("       MaxSize: (\(maxS.width), \(maxS.height))")
                }
            }
        }
    }
}
 
// All screens
for (i, screen) in NSScreen.screens.enumerated() {
    print("Screen \(i):")
    print("  frame=\(NSStringFromRect(screen.frame))")
    print("  visibleFrame=\(NSStringFromRect(screen.visibleFrame))")
    print("  safeAreaInsets.top=\(screen.safeAreaInsets.top)")
}