How to determine if an application "quit" is because of a logout or restart/shutdown?

Is it possible to distinquish if a "quit" event is being sent to an application becuase of requested logout vs. a full restart or shutdown has been requested.


I have a faceless application that monitors the status of multiple, background, processes.


When the app receives a "quit" event, how it handles those running processes and how I would like to prompt the user should be different if (a) the app is just quiting, but the user is not logging out, (b) the user is logging out but the system isn't shutting down, or (c) the system is shutting down.


I see that one can observe NSWorkspaceWillPowerOffNotification, but there doesn't seem to be any distinction between "just logout" and "logout and shutdown".

Replies

After much additional research, I think I found the answer.


In the -applicationShouldTerminate: delegate method, you can obtain the AppleEvent that triggered the quit. In it, you can extract the kEventParamReason attribute descriptor. That descriptor can be kAEShutDown, kAERestart, or kAEReallyLogOut (corresponding to the AppleEvent types one sends to intiate those actions). If the descriptor is 0, missing, or if there's no current AppleEvent, it's a user or external (kill) initiated Quit.


- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender
{
   ...
    NSAppleEventDescriptor* appleEventDesc = [[NSAppleEventManager sharedAppleEventManager] currentAppleEvent];
    NSAppleEventDescriptor* whyDesc = [appleEventDesc attributeDescriptorForKeyword:kEventParamReason];
    OSType why = [whyDesc typeCodeValue];
    if (why==kAEShutDown || why==kAERestart || why==kAEReallyLogOut)
        ...

Congrats. It is often necessary to dig deep in doc to find !


The kEventParamReason is something that could be useful in other cases.


Don't forget to mark this thread as closed on your answer.