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?
Command + Tab until you get the app's icon.
Before releasing the Command key, press and hold the Option key.
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.
Both the Command and left Option keys must be pressed on the same side (left or right) of the keyboard.
Related Topics
Programmatically Disabling Screenshot in App
Swift 2.0 Keychain Type Errors for Secitemcopymatching
Swift Access to Variable Length Array
Why Swift Closure Not Capture Self
How to Manage Swiftui State with Nested Structs
Create View Based Nstableview Programmatically Using Bindings in Swift
Lazy Property Initialization in Swift
Sktextureatlas No Longer Sharing Textures in iOS 10
Wrapping a Generic Method in a Class Extension
Add Skreferencenode/Skscene to Another Skscene in Spritekit
Spritekit and Scenekit - How to Completely Pause a Game
Cross Platform Aes Encryption Between iOS and Kotlin/Java Using Apples Cryptokit
In Swift, How to Write a Generic Function to Resize an Array
Dynamic/Runtime Dispatch in Swift, or "The Strange Way Structs Behave in One Man's Opinion"
Is the Swift Divide "/" Operator Not Working or Have I Missed Something