forked from bdrister/dterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccessibilityHelpers.swift
63 lines (51 loc) · 1.91 KB
/
AccessibilityHelpers.swift
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 Foundation
func fileAXURLStringOfAXUIElement(uiElement: AXUIElement) -> String? {
var axURL: CFTypeRef? = nil
let axErr = AXUIElementCopyAttributeValue(uiElement, kAXURLAttribute, &axURL)
if axErr != .success || axURL == nil {
return nil
}
// OK, we have some kind of AXURL attribute, but that could either be a string or a URL
if CFGetTypeID(axURL) == CFStringGetTypeID(), let axURL = axURL as? NSString, axURL.hasPrefix("file:///") {
return axURL as String
}
if CFGetTypeID(axURL) == CFURLGetTypeID(), let axURL = axURL as? URL, axURL.isFileURL {
return axURL.absoluteString
}
// unknown type...
return nil
}
func windowFrameOfAXWindow(axWindow: CFTypeRef) -> NSRect {
let axWindow = axWindow as! AXUIElement
var axErr = AXError.success
// get AXPosition of the main window
var axPosition: CFTypeRef? = nil
axErr = AXUIElementCopyAttributeValue(axWindow , kAXPositionAttribute, &axPosition)
if axErr != .success || axPosition == nil {
print ("Couln't get AXPosition: \(axErr)")
return NSZeroRect
}
// convert to CGPoint
var realAXPosition: CGPoint = CGPoint(x: 0, y: 0)
if !AXValueGetValue(axPosition as! AXValue, AXValueType(rawValue: kAXValueCGPointType)!, &realAXPosition) {
print("Couldn't extract CGPoint from AXPosition")
return NSZeroRect
}
// get AXSize
var axSize: CFTypeRef? = nil
axErr = AXUIElementCopyAttributeValue(axWindow , kAXSizeAttribute, &axSize)
if axErr != .success || axSize == nil {
print ("Couldn't get AXSize: \(axErr)")
return NSZeroRect
}
// convert to CGSize
var realAXSize: CGSize = CGSize(width: 0, height: 0)
if !AXValueGetValue(axSize as! AXValue, AXValueType(rawValue: kAXValueCGSizeType)!, &realAXSize) {
print ("Couldn't extract CGSize from AXSize")
return NSZeroRect
}
return NSRect(
origin: CGPoint(x: realAXPosition.x, y: realAXPosition.y + 20.0),
size: CGSize(width: realAXSize.width, height: realAXSize.height - 20)
)
}