Skip to content

Instantly share code, notes, and snippets.

@tylerlong
Last active August 13, 2019 09:36
Show Gist options
  • Save tylerlong/b5cee1d57920e705fa2df0b3f0990b48 to your computer and use it in GitHub Desktop.
Save tylerlong/b5cee1d57920e705fa2df0b3f0990b48 to your computer and use it in GitHub Desktop.
Make window frontmost (BUT not floating. I don't know how to make windows floating)
import Cocoa
extension AXValue {
class func initWith(t: Any) -> AXValue? {
var t = t
switch t {
case is CGPoint:
return AXValueCreate(AXValueType(rawValue: kAXValueCGPointType)!, &t)!.takeRetainedValue()
case is CGSize:
return AXValueCreate(AXValueType(rawValue: kAXValueCGSizeType)!, &t)!.takeRetainedValue()
default:
return nil
}
}
func convertTo<T>() -> T {
let ptr = UnsafeMutablePointer<T>.alloc(1)
AXValueGetValue(self, AXValueGetType(self), ptr)
let val = ptr.memory
ptr.destroy()
return val
}
}
extension AXUIElement {
func getAttribute<T>(key: String) -> T {
var ptr: AnyObject?
AXUIElementCopyAttributeValue(self, "AX\(key)", &ptr)
if key == "Size" || key == "Position" {
let val = ptr as! AXValue
return val.convertTo()
}
return ptr as! T
}
func setAttribute<T: AnyObject>(key: String, value: T) {
AXUIElementSetAttributeValue(self, "AX\(key)", value)
}
func setBounds(bounds: NSRect) { // 移动窗口并调整窗口大小
setAttribute("Position", value: AXValue.initWith(bounds.origin)!)
setAttribute("Size", value: AXValue.initWith(bounds.size)!)
}
}
import Cocoa
class AXWindow {
let app: AXUIElement
let window: AXUIElement
init(app: AXUIElement, window: AXUIElement) {
self.app = app
self.window = window
}
class func initWithCGWindow(cgWindow: CGWindow) -> AXWindow? {
let app = AXUIElementCreateApplication(pid_t(cgWindow.ownerPID)).takeRetainedValue();
let windows: [AXUIElement] = app.getAttribute("Windows")
for window in windows {
let title: String = window.getAttribute("Title")
if title != cgWindow.name {
continue
}
let point: NSPoint = window.getAttribute("Position")
if point != cgWindow.bounds.origin {
continue
}
let size: NSSize = window.getAttribute("Size")
if size != cgWindow.bounds.size {
continue
}
return AXWindow(app: app, window: window)
}
return nil
}
func setPosition(view: NSView) {
let screenRect = view.screenRect()
self.window.setBounds(screenRect)
toFrontQueue.push(self)
}
func bringToFront() {
Swift.print("bringToFront")
self.app.setAttribute("Frontmost", value: true)
self.window.setAttribute("Main", value: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment