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 a different app like Xcode would trigger this event.
Is there any way to pick up application switching events, like through an event monitor perhaps?
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)
}
}
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...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With