pyobjc / Examples / autoreadme.py

The branch 'pyobjc-ancient' does not exist.
#!/usr/bin/python
"""
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: 
    http://www.macdevcenter.com/pub/a/mac/2003/01/31/pyobjc_one.html
"""
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)
                    return
                workspace.openFile_withApplication_(fullPath, app)
            
notificationHandler = NotificationHandler.new()
notificationCenter.addObserver_selector_name_object_(notificationHandler,
                                                     "handleMountNotification:",
                                                     NSWorkspaceDidMountNotification,
                                                     None)

NSLog("Listening for mount notifications....")
NSRunLoop.currentRunLoop().run() # never exits
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.