Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@c9iim
Last active December 16, 2017 23:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save c9iim/0fcf80ad6b7cac8b8025 to your computer and use it in GitHub Desktop.
Save c9iim/0fcf80ad6b7cac8b8025 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