Skip to content

Instantly share code, notes, and snippets.

@gregneagle
Created August 1, 2017 18:20
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gregneagle/94a400a7bce6f8cfc9e7eff87270c9e9 to your computer and use it in GitHub Desktop.
Save gregneagle/94a400a7bce6f8cfc9e7eff87270c9e9 to your computer and use it in GitHub Desktop.
Getting notified when a preference changes (using PyObjC)
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