Monitoring app switching on OS X

前端 未结 2 1667
半阙折子戏
半阙折子戏 2020-12-17 00:56

I want to know, from within a Swift application, when the user changes from one application to another, just in general.

For example: switching from Google Chrome to

相关标签:
2条回答
  • 2020-12-17 01:34

    In Swift 3 you need to use the keyword let for each optional binding. Also, the running application, as extracted from the userInfo dictionary, is of type any, and it needs to be further cast to the NSRunningApplication type.

    So the Swift 3 answer given by Eric Aya works but needs small modifications:

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        NSWorkspace.shared().notificationCenter.addObserver(self,
                                                            selector: #selector(activated),
                                                            name: NSNotification.Name.NSWorkspaceDidActivateApplication,
                                                            object: nil)
    }
    
    func activated(notification: NSNotification) {
        if let info = notification.userInfo,
            let app = info[NSWorkspaceApplicationKey] as? NSRunningApplication,
            let name = app.localizedName {
                print(name)
        }
    }
    

    (I would have left this post as comment to the accepted answer, but not enough rep...)

    0 讨论(0)
  • 2020-12-17 01:43

    You can add an observer on NSWorkspace.sharedWorkspace().notificationCenter watching for the NSWorkspaceDidActivateApplicationNotification key. You point the selector at one of your methods and grab the information from the userInfo dictionary.

    Simple example in AppDelegate:

    Swift 2.2

    func applicationDidFinishLaunching(notification: NSNotification) {
        NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self,
                                                            selector: #selector(activated),
                                                            name: NSWorkspaceDidActivateApplicationNotification,
                                                            object: nil)
    }
    
    func activated(notification: NSNotification) {
        if let info = notification.userInfo,
            app = info[NSWorkspaceApplicationKey],
            name = app.localizedName {
                print(name)
        }
    }
    

    Swift 3

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        NSWorkspace.shared().notificationCenter.addObserver(self,
                                                            selector: #selector(activated(_:)),
                                                            name: NSNotification.Name.NSWorkspaceDidActivateApplication,
                                                            object: nil)
    }
    
    func activated(_ notification: NSNotification) {
        if let info = notification.userInfo,
            let app = info[NSWorkspaceApplicationKey] as? NSRunningApplication,
            let name = app.localizedName 
        {
            print(name)
        }
    }
    
    0 讨论(0)
提交回复
热议问题