Monitoring App Switching on Os X

Monitoring app switching on OS X

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)
}
}

How do I monitor processes/apps or file locks on Mac OSX?

What I wound up doing, was to listen to Notifications from NSNotificationCenter and NSApplication after I had asked the OS to open my file in its default Application. The particular events were:

  • NSWorkspace.Notifications.ObserveDidLaunchApplication(EventHandler)
  • NSWorkspace.Notifications.ObserveDidTerminateApplication(EventHandler)

If you're stuck with the same issue, please see my implementation here.

Mac OS X - How to monitor a window change event?

The closest you'll get will probably be the accessibility APIs. See here and here for similar questions with (partial) solutions.

Full screen apps keep switching places

Go to System Preferences > Mission Control and make sure that "Automatically rearrange spaces based on recent use" is not ticked.

Keyboard shortcut for restoring applications from the Mac OS X Dock?


  1. Command + Tab until you get the app's icon.

  2. Before releasing the Command key, press and hold the Option key.


  1. You must switch to another app and let it take focus first. In other words, you can't just Command + Tab to another app and before actually selecting that app (by releasing the Command and Tab keys), switch right back to your minimized app, which you might attempt to do if you minimized it by accident or just simply changed your mind shortly after minimizing.

  2. Both the Command and left Option keys must be pressed on the same side (left or right) of the keyboard.



Related Topics



Leave a reply



Submit