Created
March 11, 2019 17:51
-
-
Save emilyst/c98ad1cbdbe8c15db87eb8211e0a7277 to your computer and use it in GitHub Desktop.
Log sleep/wake notifications on macOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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