Ariver
2026-06-18 16ee4576a9ff3c4f91cc30cb4cedbfb41fb3de14
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
import AppKit
import CoreGraphics
import Foundation
 
guard CommandLine.arguments.count == 2,
      let screenIndex = Int(CommandLine.arguments[1])
else {
    fputs("usage: round1-move-mouse-to-screen-center.swift <screen-index>\n", stderr)
    exit(2)
}
 
let screens = NSScreen.screens
guard screens.indices.contains(screenIndex) else {
    fputs("screen index \(screenIndex) is out of range; screen count=\(screens.count)\n", stderr)
    exit(2)
}
 
let screen = screens[screenIndex]
let displayID = screen.deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as? CGDirectDisplayID ?? 0
let coreGraphicsFrame = displayID == 0 ? screen.frame : CGDisplayBounds(displayID)
let coreGraphicsPoint = CGPoint(x: coreGraphicsFrame.midX, y: coreGraphicsFrame.midY)
 
CGWarpMouseCursorPosition(coreGraphicsPoint)
CGAssociateMouseAndMouseCursorPosition(boolean_t(1))
usleep(100_000)
 
print(
    "screen=\(screenIndex) " +
    "frame=x:\(Int(screen.frame.minX)),y:\(Int(screen.frame.minY)),w:\(Int(screen.frame.width)),h:\(Int(screen.frame.height)) " +
    "cgFrame=x:\(Int(coreGraphicsFrame.minX)),y:\(Int(coreGraphicsFrame.minY)),w:\(Int(coreGraphicsFrame.width)),h:\(Int(coreGraphicsFrame.height)) " +
    "mouse=x:\(Int(NSEvent.mouseLocation.x)),y:\(Int(NSEvent.mouseLocation.y))"
)