Last active
December 16, 2017 23:40
-
-
Save c9iim/0fcf80ad6b7cac8b8025 to your computer and use it in GitHub Desktop.
AXUIElement in Swift pert2
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 | |
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