Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

macOS: Is there any way to know when the user has tried to quit an application via its Dock icon?

Is there any way for a Cocoa application to detect when the user has tried to quit it via its Dock menu, and not by some other method?

Normally it's possible to catch and respond to quit events using the application delegate's applicationShouldTerminate: method. However, this method doesn't seem to distinguish between the request to quit coming from the application's main menu, from its Dock icon, from an Apple event, or any other conventional method of quitting the application. I'm curious if there's any way to know precisely how the user has tried to quit the application.

like image 611
Bri Bri Avatar asked Jul 16 '19 19:07

Bri Bri


People also ask

How do I stop apps from staying on my Mac Dock?

Open System Preferences, Click on dock, uncheck "Show recent applications in dock". This should probably solve your problem.

Should I always quit apps on Mac?

You don't have to quit apps once you're done working in them — OS X allows you to work with several apps open at once. Features such as Compressed Memory and App Nap keep your Mac running fast, and save power when many apps are open.

What does black dot under icons on Mac mean?

The black dots appear under the apps in the Dock because Apple wants the user to be aware of apps that are still running in the background, regardless of whether or not you can see them doing so. The black dot, essentially, is so that you can see that the app you recently closed is still running.

How do I force quit notability on Mac?

If you can't force the app to quit Restart your Mac: Choose Apple menu  > Restart. If you can't restart because your Mac is not responding, force your Mac to turn off: Press and hold the power button on your Mac for up to 10 seconds, until your Mac turns off. Every Mac has a power button.


1 Answers

It is in fact possible for an app to know the reason why it's quitting by checking to see if there is current AppleEvent being handled and, if so, checking to see whether it's a quit event and whether it was the Dock that sent it. (See this thread discussing how to tell if an app is being quit because the system is logging out or shutting down.)

Here is an example of a method that, when called from the application delegate's applicationShouldTerminate: method, will return true if the app is being quit via the Dock:

- (bool)isAppQuittingViaDock {
    NSAppleEventDescriptor *appleEvent = [[NSAppleEventManager sharedAppleEventManager] currentAppleEvent];

    if (!appleEvent) {
        // No Apple event, so the app is not being quit by the Dock.
        return false;
    }

    if ([appleEvent eventClass] != kCoreEventClass || [appleEvent eventID] != kAEQuitApplication) {
        // Not a 'quit' event
        return false;
    }

    NSAppleEventDescriptor *reason = [appleEvent attributeDescriptorForKeyword:kAEQuitReason];  

    if (reason) {
        // If there is a reason for this 'quit' Apple event (such as the current user is logging out)
        // then it didn't occur because the user quit the app through the Dock.
        return false;
    }

    pid_t senderPID = [[appleEvent attributeDescriptorForKeyword:keySenderPIDAttr] int32Value];

    if (senderPID == 0) {
        return false;
    }

    NSRunningApplication *sender = [NSRunningApplication runningApplicationWithProcessIdentifier:senderPID];

    if (!sender) {
        return false;
    }

    return [@"com.apple.dock" isEqualToString:[sender bundleIdentifier]];
}
like image 124
Bri Bri Avatar answered Sep 28 '22 03:09

Bri Bri