Skip to content

Instantly share code, notes, and snippets.

@emilyst
Created March 11, 2019 17:51
Show Gist options
  • Save emilyst/c98ad1cbdbe8c15db87eb8211e0a7277 to your computer and use it in GitHub Desktop.
Save emilyst/c98ad1cbdbe8c15db87eb8211e0a7277 to your computer and use it in GitHub Desktop.
Log sleep/wake notifications on macOS
#!/usr/bin/python
from AppKit import NSWorkspace, \
NSWorkspaceWillSleepNotification, \
NSWorkspaceDidWakeNotification, \
NSObject, \
NSLog
from PyObjCTools import AppHelper
class NotificationHandler(NSObject):
def receiveSleepNotification_(self, notification):
NSLog('receiveSleepNotification: %@', notification)
def receiveWakeNotification_(self, notification):
NSLog('receiveWakeNotification: %@', notification)
workspace = NSWorkspace.sharedWorkspace()
notificationCenter = workspace.notificationCenter()
notificationHandler = NotificationHandler.new()
notificationCenter.addObserver_selector_name_object_(
notificationHandler,
'receiveSleepNotification:',
NSWorkspaceWillSleepNotification,
None
)
notificationCenter.addObserver_selector_name_object_(
notificationHandler,
'receiveWakeNotification:',
NSWorkspaceDidWakeNotification,
None
)
NSLog('Listening for sleep notifications....')
AppHelper.runConsoleEventLoop(installInterrupt=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment