Call applicationShouldTerminate during logout

Is there any way to call a handler in a running ASOC app when the user logs out of their account? The app just seems to get killed without running applicationShouldTerminate before it quits.

How does one implement these final tasks for a running application during log out?

Model: Mac Pro 3.2 GHz Quad-Core Intel Xeon
Browser: Safari 536.26.17
Operating System: Mac OS X (10.8)

This is just a thought but is there anywhere I can capture the Logout / Restart / Shutdown event and make it run a script to gracefully quit my running ASOC application rather than the event just killing it?

I’m not entirely convinced that applicationShouldTerminate isn’t being called. Put a logging message in it to check.

You might need to do something like this (written in the browser, so check it):

on applicationShouldTerminate:notif
my performSelector:"cleanUp" withObject:(missing value) afterDelay:0.0
return current application's NSTerminateLater
end applicationShouldTerminate:

on cleanUp()
-- do clean up
current application's NSApp's replyToApplicationShouldTerminate:true
end cleanUp

Okay I see that it is getting called. My issue now is that my app is not user interactive and I have added

to the Info.plist within the package contents of finished app so it does not show in dock. Once I do this the log out event does not wait for my app to finish the clean up handler.

My question now is how do I get the log out event to wait till my app has finished quitting?
I have been trying to use.

return current application’s NSTerminateLater
current application’s NSApp’s replyToApplicationShouldTerminate_(true)

without success…

It may well be that once you set LSUIElement, you’re expected to support sudden termination.

If I add an observer for a notification NSWorkspaceWillPowerOffNotification is this the correct way to do this?
I don’t have LSUIElement on at this point…

on startObserving_(sender)
tell current application's NSWorkspace to set theWorkspace to sharedWorkspace()
set notifCenter to theWorkspace's notificationCenter()
tell notifCenter to addObserver_selector_name_object_(me, "appWillCleanUp:", "NSWorkspaceWillPowerOffNotification", missing value)
    end startObserving_
on stopObserving_(sender)
tell current application's NSWorkspace to set theWorkspace to sharedWorkspace()
set notifCenter to theWorkspace's notificationCenter()
tell notifCenter to removeObserver_name_object_(me, "NSWorkspaceWillPowerOffNotification", missing value)
end stopObserving_

on applicationWillFinishLaunching_(aNotification)
end applicationWillFinishLaunching_

on appWillCleanUp_(notif)
display dialog "appWillCleanUp_ Called"
end appWillCleanUp_

Looks fine – is it doing what you want?

On my applicationWillFinishLaunching_ I changed sender to “me” and it puts up the dialog and logout will stop until I click OK but as soon as I add

it still puts up the dialog but doesn’t care and logs out immediately before clean-up can complete.

I’m wondering if I programmatically set icon to hidden if it would do the same. Been working with NSApplicationActivationPolicyAccessory but can’t seem to get the terminology correct. Any suggestions?

It looks read-only to me.

The only thing I found I could do is write information to the users defaults then when the application is launched again get the information from the user default and work with it to do the tasks I wanted it to do before logout event.
I’m sure this in not the way to perform a clean-up and writing log files with a daemon. Launchd doesn’t even have anything for logout or shut down to even work with and login hooks are deprecated. What is there to use?
I really can’t figure out how to give a daemon, using LSUIElement, a chance to quit gracefully. If anyone has this information it would be good to know…