Created
August 1, 2017 18:20
-
-
Save gregneagle/94a400a7bce6f8cfc9e7eff87270c9e9 to your computer and use it in GitHub Desktop.
Getting notified when a preference changes (using PyObjC)
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
from Foundation import NSObject, NSUserDefaults, NSKeyValueObservingOptionNew | |
from Foundation import NSRunLoop, NSDate | |
class PrefsObserver(NSObject): | |
def observe(self, domain, key): | |
self.domain = domain | |
self.key = key | |
if self: | |
self.defaults = NSUserDefaults.alloc().initWithSuiteName_( | |
self.domain) | |
self.defaults.addObserver_forKeyPath_options_context_( | |
self, self.key, NSKeyValueObservingOptionNew, None) | |
return self | |
def __del__(self): | |
self.defaults.removeObserver_forKeyPath_(self, self.key) | |
def observeValueForKeyPath_ofObject_change_context_( | |
self, keyPath, object, change, context): | |
print change | |
observer = PrefsObserver.alloc().init().observe( | |
'ManagedInstalls', 'LastCheckDate') | |
while True: | |
NSRunLoop.currentRunLoop( | |
).runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(0.3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment