[Pyobjc-dev] Attempt at writing Cocoa services with pyobjc
Brought to you by:
ronaldoussoren
From: Dinu G. <gh...@da...> - 2003-01-06 12:42:18
|
Hi, After recently seeing the release of pyobjc 0.8 I decided it was high time to have some fun writing *real* Python Cocoa programs! I started using pyobjc on a little Cocoa Services tool, much in the spirit of URLServices for those who know it. Unfortunately, I can't find a URL for downloading it, but I can provide my copy of it to anybody interested. There are two problems I'm having so far with that first toy pro- ject, after just creating a "Python Cocoa App." with PB: 1. Removing the PB Resources folder containing the MainMenu.nib and InfoPlist.strings results in complaints like "Unable to load nib file: MainMenu, exiting". The NIB file isn't needed in this case as there is neither a GUI nor an app "owner" object, hence I also re- moved the MyAppDelegate.py thinking that would be ok. Apparently there must be something else to get rid of the need for a NIB file. Any ideas? 2. In the Python main file I fumbled to port the code from the URLServices project inside this function: int main(int argc, const char **argv) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; URLServices *serviceProvider = [[URLServices alloc] init]; NSRegisterServicesProvider(serviceProvider, @"URLServices"); NS_DURING [[NSRunLoop currentRunLoop] configureAsServer]; [[NSRunLoop currentRunLoop] run]; NS_HANDLER NSLog(@"%@", localException); NS_ENDHANDLER [serviceProvider release]; [pool release]; exit(0); return 0; } to something like the following: # import classes required to start application import PyServices from Foundation import NSAutoreleasePool, NSRunLoop, NSLog from AppKit import NSRegisterServicesProvider # stuff ported from URLServices' main.m ## pool = NSAutoreleasePool.alloc().init() serviceProvider = PyServices.PyServices.alloc().init() NSRegisterServicesProvider(serviceProvider, "PyServices") # NS_DURING NSRunLoop.currentRunLoop().configureAsServer() NSRunLoop.currentRunLoop().run() # NS_HANDLER NSLog("%@", localException) # NS_ENDHANDLER serviceProvider.release() ## pool.release() # pass control to the AppKit (from template __main__.py) # import sys # sys.exit(AppKit.NSApplicationMain(sys.argv)) where it seems that using the autorelease pool is a bad idea (com- plains with "Cannot retain an autorelease pool") and replacing the NSApplicationMain function with an NSRunLoop does not quite seem to work like expected, resulting in crash reports being dumped. I'm barely beginning to really play with pyobjc and so I don't have a detailed idea yet of how it works, while at the same time I'm un- dusting some Cocoa knowledge... Since it is only some 15 KB I dare to attach my initial code here right away. If anybody can give a helping hand this could be a nice demo for pyobjc since text, URL and file management is *much* easier in Py- thon than in the Foundation Kit... Regards, Dinu PS: BTW, this is Apple's online info about how to create services: http://developer.apple.com/techpubs/macosx/Cocoa/TasksAndConcepts/ ProgrammingTopics/SysServices/index.html -- Dinu C. Gherman ...................................................................... "Women give to men the very gold of their lives. But they invariably want it back in small change." (Oscar Wilde) |