pyobjc / Examples /

The branch 'pyobjc-ancient' does not exist.
Full commit
This script is a daemon that will open the ReadMe file in the root of any
(removable) volume that is inserted while this script is running.

The script is part of an article at MAcDevCenter:
import sys
import os
import re

from AppKit import NSWorkspace, NSWorkspaceDidMountNotification
from Foundation import *

workspace = NSWorkspace.sharedWorkspace()
notificationCenter = workspace.notificationCenter()

readTheseFiles = re.compile('(.*read\s*me.*|.*release.*note.*|^about.*)', re.I)

class NotificationHandler(NSObject):
    def handleMountNotification_(self, aNotification):
        path = aNotification.userInfo()['NSDevicePath']
        for aFile in os.listdir(path):
            if readTheseFiles.match(aFile):
                fullPath = os.path.join(path, aFile)
                success, app, None = workspace.getInfoForFile_application_type_(fullPath)
                if not success:
                    NSLog("Failed to find application to open file %s" % fullPath)
                workspace.openFile_withApplication_(fullPath, app)
notificationHandler =

NSLog("Listening for mount notifications....")
NSRunLoop.currentRunLoop().run() # never exits