Is there a programmatic way to monitor when a Mac is about the to go sleep? I would like to monitor when the user deliberately tells the system to go to sleep and when the system is preparing to sleep naturally.
You can register for the following notification like this:
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(receiveSleepNotification:) name:NSWorkspaceScreensDidSleepNotification object:NULL];
You can register for a wake notification like this:
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(receiveWakeNotification:) name:NSWorkspaceDidWakeNotification object:NULL];