Last active
August 29, 2015 14:50
-
-
Save uchcode/716be5207a3cd9c8d57e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Cocoa | |
protocol AXWindowArray { | |
static func getWindows(processIdentifier pid: pid_t) -> [AXUIElement] | |
static func getWindows(bundleIdentifier bid: String) -> [AXUIElement] | |
} | |
extension AXWindowArray { | |
static func getWindows(processIdentifier pid: pid_t) -> [AXUIElement] { | |
let elm = AXUIElementCreateApplication(pid).takeUnretainedValue() | |
let val = UnsafeMutablePointer<AnyObject?>.alloc(1) | |
AXUIElementCopyAttributeValue(elm, kAXWindowsAttribute, val) | |
let ary = val.memory as? [AXUIElement] ?? [] | |
val.dealloc(1) | |
return ary | |
} | |
static func getWindows(bundleIdentifier bid: String) -> [AXUIElement] { | |
let ary : [AXUIElement]! | |
let run = NSWorkspace.sharedWorkspace().runningApplications | |
if let app = run.filter({ $0.bundleIdentifier == bid }).first { | |
ary = Self.getWindows(processIdentifier: app.processIdentifier) | |
} else { | |
ary = [] | |
} | |
return ary | |
} | |
} | |
extension AXWindowArray where Self : NSRunningApplication { | |
var windows : [AXUIElement] { | |
return Self.getWindows(processIdentifier: self.processIdentifier) | |
} | |
} | |
extension NSRunningApplication : AXWindowArray { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment