修复 MacBook Neo 光标延迟:每 10 秒记录 1 像素屏幕
#! /bin/bash set -Efeuxo pipefail APP= "Unlag Neo" DISPLAY_NAME= "Unlag Neo" if [ -e "${APP}.app" ] ; then echo "错误:${APP}.app 已存在" exit 1 fi mkdir -vp "${APP}.app/Contents/MacOS" "${APP}.app/Contents/Resources" PATH= "$PATH:/usr/libexec" swiftc - -o "${APP}.app/Contents/MacOS/${APP}" << 'SWIFT' import Foundation import AppKit import CoreGraphics import CoreMedia import ServiceManagement import ApplicationServices @preconcurrency import ScreenCaptureKit struct CaptureError: Error, CustomStringConvertible { let description: String init(_ description: String) { self.description = description } } @MainActor var showedPermissionAlert = false @MainActor var showedAccessibilityAlert = false @MainActor func relaunchUnlag() { let isApp = Bundle.main.bundlePath.hasSuffix(".app") let target = isApp ? Bundle.main.bundlePath : (Bundle.main.executablePath ?? Bundle.main.bundlePath) let p = Process() p.executableURL = URL(fileURLWithPath: "/bin/sh") p.arguments = ["-c", isApp ? "sleep 0.5; /usr/bin/open \"$1\"" : "sleep 0.5; \"$1\" >/dev/null 2>&1 &", "sh", target] try? p.run() NSApp.terminate(nil) } @MainActor func openScreenRecordingSettings() { NSWorkspace.shared.open(URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture")!) } @MainActor func openAccessibilitySettings() { NSWorkspace.shared.open(URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility")!) } @MainActor func showPermissionAlert() { NSApp.activate(ignoringOtherApps: true) let a = NSAlert() a.alertStyle = .warning a.messageText = "需要屏幕录制权限" a.informativeText = "请在系统设置中授予屏幕录制权限,然后重启 Unlag Neo。" a.addButton(withTitle: "打开设置") a.addButton(withTitle: "重启 Unlag Neo") a.addButton(withTitle: "退出 Unlag Neo") switch a.runModal() { case .alertFirstButtonReturn: openScreenRecordingSettings() let b = NSAlert() b.alertStyle = .informational b.messageText = "在权限后重启 Unlag Neo" b.informativeText = "启用屏幕录制权限后,重启 Unlag Neo。" b.addButton(withTitle: "重启 Unlag Neo") b.addButton(withTitle: "退出 Unlag Neo") if b.runModal() == .alertFirstButtonReturn { relaunchUnlag() } else { NSApp.terminate(nil) } case .alertSecondButtonReturn: relaunchUnlag() default: NSApp.terminate(nil) } } @MainActor func showAccessibilityAlert() { guard !showedAccessibilityAlert else { return } showedAccessibilityAlert = true NSApp.activate(ignoringOtherApps: true) let a = NSAlert() a.alertStyle = .warning a.messageText = "需要辅助功能权限" a.informativeText = "请在系统设置中授予辅助功能权限,然后重启 Unlag Neo。" a.addButton(withTitle: "打开设置") a.addButton(withTitle: "重启 Unlag Neo") a.addButton(withTitle: "退出 Unlag Neo") switch a.runModal() { case .alertFirstButtonReturn: openAccessibilitySettings() let b = NSAlert() b.alertStyle = .informational b.messageText = "在权限后重启 Unlag Neo" b.informativeText = "启用辅助功能权限后,重启 Unlag Neo。" b.addButton(withTitle: "重启 Unlag Neo") b.addButton(withTitle: "退出 Unlag Neo") if b.runModal() == .alertFirstButtonReturn { relaunchUnlag() } else { NSApp.terminate(nil) } case .alertSecondButtonReturn: relaunchUnlag() default: NSApp.terminate(nil) } } @MainActor func requireScreenRecordingPermission() throws { guard !CGPreflightScreenCaptureAccess() else { return } _ = CGRequestScreenCaptureAccess() if !showedPermissionAlert { showedPermissionAlert = true showPermissionAlert() } throw CaptureError("缺少屏幕录制权限。") } func axCopy(_ element: AXUIElement, _ attr: String) -> AnyObject? { var value: CFTypeRef? return AXUIElementCopyAttributeValue(element, attr as CFString, &value) == .success ? value : nil } func axBool(_ element: AXUIElement, _ attr: String) -> Bool { (axCopy(element, attr) as? NSNumber)?.boolValue ?? false } func axValue(_ element: AXUIElement, _ attr: String, _ type: AXValueType) -> AXValue? { guard let v = axCopy(element, attr) else { return nil } let axv = v as! AXValue return AXValueGetType(axv) == type ? axv : nil } func axRect(_ element: AXUIElement) -> CGRect? { guard let p = axValue(element, kAXPositionAttribute, .cgPoint), let s = axValue(element, kAXSizeAttribute, .cgSize) else { return nil } var point = CGPoint.zero var size = CGSize.zero guard AXValueGetValue(p, .cgPoint, &point), AXValueGetValue(s, .cgSize, &size) else { return nil } return CGRect(origin: point, size: size) } func focusedOrMainWindow(_ app: AXUIElement) -> AXUIElement? { if let w = axCopy(app, kAXFocusedWindowAttribute) { return (w as! AXUIElement) } if let w = axCopy(app, kAXMainWindowAttribute) { return (w as! AXUIElement) } return nil } func axWindows(_ app: AXUIElement) -> [AXUIElement] { guard let xs = axCopy(app, kAXWindowsAttribute) as? [AnyObject] else { return [] } return xs.map { $0 as! AXUIElement } }
本站免费、广告极少。如果觉得有帮助,可以请我们喝杯咖啡 —— 任何金额都对持续运营有实际帮助。
☕请我喝杯咖啡