import os, sys,re
import time
from threading import Thread
from Queue import Queue
from AppKit import NSApplication, NSWorkspace, NSWorkspaceDidMountNotification, NSWorkspaceDidUnmountNotification, NSAutoreleasePool
from Foundation import NSObject
import objc
class NotificationHandler(NSObject):
def _setQueueOut(self,queueOut):
self.queueOut=queueOut
def handleNotification(self, notification):
path = notification.userInfo()['NSDevicePath']
name = notification.name()
if name == NSWorkspaceDidMountNotification:
notificationName = "mounted"
elif name == NSWorkspaceDidUnmountNotification:
notificationName = "unmounted"
print notificationName
self.queueOut.put(["cd_"+notificationName,path])
class MacOsXCdNotification(Thread):
def __init__(self,queueOut):
Thread.__init__(self)
self.queueOut = queueOut
self.setDaemon(True)
def run(self):
# Create an instance of our notification handler, and ask the workspace
# notification center to tell us when a new volume is mounted.
pool = NSAutoreleasePool.alloc().init()
notifier = NotificationHandler.alloc().init()
notifier._setQueueOut(self.queueOut)
ws = NSWorkspace.sharedWorkspace()
ns = ws.notificationCenter()
ns.addObserver_selector_name_object_(notifier, notifier.handleNotification, NSWorkspaceDidMountNotification, None)
ns.addObserver_selector_name_object_(notifier, notifier.handleNotification, NSWorkspaceDidUnmountNotification, None)
print "start"
self.queueOut.put(["cd_detect_start"])
NSApplication.sharedApplication().run()
del pool
if __name__ == "__main__":
print "detection started"
q = Queue(1000)
c = MacOsXCdNotification(q)
c.start()
while True:
print q.get()