Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hexfire/a03a08dc9cf5d3858d827fd6086b3a76 to your computer and use it in GitHub Desktop.
Save Hexfire/a03a08dc9cf5d3858d827fd6086b3a76 to your computer and use it in GitHub Desktop.
AXUIElement in Swift pert2
import Cocoa
extension NSWorkspace {
class func frontmostApp() -> NSRunningApplication? {
return self.sharedWorkspace().frontmostApplication
}
class func runningApp(bundleIdentifier:NSString) -> NSRunningApplication? {
let runningApplications = NSWorkspace.sharedWorkspace().runningApplications
return runningApplications.filter({$0.bundleIdentifier == bundleIdentifier}).first
}
}
extension NSRunningApplication {
func windows() -> [AXUIElement] {
let windowList : UnsafeMutablePointer<AnyObject?> = UnsafeMutablePointer<AnyObject?>.alloc(1)
let appRef = AXUIElementCreateApplication(self.processIdentifier).takeRetainedValue()
AXUIElementCopyAttributeValue(appRef, "AXWindows", windowList)
return windowList.memory as! [AXUIElement]
}
}
class ApplicationDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) {
var windows : [AXUIElement]?
// Get Active Applilcation
if let app = NSWorkspace.frontmostApp() {
windows = app.windows()
NSLog("windows: \(windows)")
}
// Get Applilcation by bundleIdentifier
if let app = NSWorkspace.runningApp("com.apple.finder") {
windows = app.windows()
NSLog("windows: \(windows)")
}
//
NSRunningApplication.currentApplication().terminate()
}
}
let applicationDelegate = ApplicationDelegate()
let application = NSApplication.sharedApplication()
application.setActivationPolicy(NSApplicationActivationPolicy.Accessory)
application.delegate = applicationDelegate
application.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment