[Pythonmac-SIG] AE, carbon and os X

Bob Ippolito bob at redivi.com
Wed Sep 10 03:26:19 EDT 2003


On Wednesday, Sep 10, 2003, at 01:10 America/New_York, Stéphane  
Jolicoeur-Fidelia wrote:

> Hello,
>
> I have been reading through about 2 years worth of the archives, in  
> search of tips/tutorials on how to use python to script OS X apps. Now  
> I have found alot of:
> import App
> d = App.App()
> do stuff with d
> etc ...
>
> but for some reason these do not work. I use both the pre-installed  
> python and MacPython from VisionEgg, on OS X.2.6 . Also I cannot seem  
> to find any info on Carbon libs, from what I gather from there names,  
> I should be able to create GUI apps but right now I am just coding  
> blind with dir().
>
> I would also like to know if it is possible to know of events like  
> disk loading, image mounting and the likes?
>
> Tutorials on a web page would greatly be apreciated by many others I  
> am sure.

As far as creating GUI apps goes, what you probably want to use is  
PyObjC ( http://pyobjc.sourceforge.net/ ), not Carbon.  There's more  
than one way to do just about everything on MacOS X (low level API,  
Carbon level API, Cocoa level API) and some of these things are wrapped  
in more than one way from Python.  You generally want to do things at  
the Cocoa level, because things are generally straight forward and easy  
that way.

In order to cause the registered application for a file to open said  
file, you can use my LaunchServices module (  
http://undefined.org/python/ -- I suggest using PackageManager to  
download from my repository).
	import LaunchServices
	LaunchServices.OpenPath('somefile')
	LaunchServices.OpenURL('http://someurl/')
LaunchServices.OpenPath('somediskimage') or  
LaunchServices.OpenURL('file://somediskimage') is an easy way to mount  
a disk image.

This is an example of a low level API wrapping from Python.

Using PyObjC, you can do the same (see  
http://developer.apple.com/documentation/Cocoa/Reference/ 
ApplicationKit/ObjC_classic/Classes/NSWorkspace.html ):
	from AppKit import NSWorkspace
	ws = NSWorkspace.sharedWorkspace()
	ws.openFile_('somefile')
	ws.openFile_('http://someURL/')

This is an example of a Cocoa level API wrapping from Python.

Note that to use most things from AppKit (or Carbon) you must have  
Window Manager access (either by running from an app bundle, or using  
the pythonw shell script to start python).

One of the easiest ways to receive notifications of disk mounting and  
unmounting is also through PyObjC:
     from AppKit import NSApplication, NSWorkspace,  
NSWorkspaceDidMountNotification, NSWorkspaceDidUnmountNotification
     from Foundation import NSFileManager, NSObject
     from PyObjCTools import AppHelper

     class MyNotifier(NSObject):
         def printNotification(self, notification):
             path = notification.userInfo()['NSDevicePath']
             name = notification.name()
             if name == NSWorkspaceDidMountNotification:
                 displayname = "mounted"
             elif name == NSWorkspaceDidUnmountNotification:
                 displayname = "unmounted"
             print "Device at path: %s with name: %s %s" % (path,  
fm.displayNameAtPath_(path), displayname)

     notifier = MyNotifier.alloc().init()
     ws = NSWorkspace.sharedWorkspace()
     fm = NSFileManager.defaultManager()
     ns = ws.notificationCenter()
     ns.addObserver_selector_name_object_(notifier,  
notifier.printNotification, NSWorkspaceDidMountNotification, None)
     ns.addObserver_selector_name_object_(notifier,  
notifier.printNotification, NSWorkspaceDidUnmountNotification, None)
     NSApplication.sharedApplication().run()

Feel free to reproduce this on a web page if you want it on one, but  
I'm lazy at the moment and I don't know of any MacPython wikis to throw  
this example code on :)

-bob




More information about the Pythonmac-SIG mailing list