Created
July 25, 2020 20:11
-
-
Save nemolize/a4f704249ccf28aced22ddf0adcaead6 to your computer and use it in GitHub Desktop.
These function demonstrates how to change coordinate of the focused window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func moveWindow(_ appRef: AXUIElement, _ velocity: CGPoint) { | |
do { | |
var windowRef: AnyObject? | |
try appRef.copyAttributeValue(kAXFocusedWindowAttribute, &windowRef) | |
let windowElement: AXUIElement = windowRef as! AXUIElement | |
var positionRef: CFTypeRef? | |
try windowElement.copyAttributeValue(kAXPositionAttribute, &positionRef) | |
var position = CGPoint() | |
if !AXValueGetValue(positionRef as! AXValue, AXValueType.cgPoint, &position) { | |
throw AppError.accessibility("AXValueGetValue has failed") | |
} | |
position += velocity | |
if let positionAxValue = AXValueCreate(AXValueType.cgPoint, &position) { | |
try windowElement.setAttributeValue(kAXPositionAttribute, positionAxValue) | |
} | |
} catch let AppError.accessibility(message, code) { | |
debugPrint(message, code ?? "none") | |
} catch { | |
debugPrint("Unknown error has occurred.") | |
} | |
} | |
extension AXUIElement { | |
func copyAttributeValue(_ attribute: String, _ destination: UnsafeMutablePointer<CFTypeRef?>) throws { | |
let result = AXUIElementCopyAttributeValue(self, attribute as CFString, destination) | |
if result != AXError.success { | |
throw AppError.accessibility("AXUIElementCopyAttributeValue has failed.", result.rawValue) | |
} | |
} | |
func setAttributeValue(_ attribute: String, _ value: AXValue) throws { | |
let result = AXUIElementSetAttributeValue(self, attribute as CFString, value) | |
if result != AXError.success { | |
throw AppError.accessibility("AXUIElementSetAttributeValue has failed", result.rawValue) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment