// // AppDelegate.m // PyRu // // Created by Bill Bumgarner on 11/24/07. // Copyright 2007 __MyCompanyName__. All rights reserved. // #import "AppDelegate.h" @class PythonStuff; @interface NSObject (MethodsThatReallyDoExist) - (NSArray *) arrayOfNamedStrings; @end @implementation AppDelegate - (void)applicationDidFinishLaunching: (NSApplication *)anApp { NSBundle *mainBundle = [NSBundle mainBundle]; NSString *resourcePath = [mainBundle resourcePath]; NSArray *pythonPathArray = [NSArray arrayWithObjects: resourcePath, [resourcePath stringByAppendingPathComponent:@"PyObjC"], nil]; setenv("PYTHONPATH", [[pythonPathArray componentsJoinedByString:@":"] UTF8String], 1); NSArray *possibleMainExtensions = [NSArray arrayWithObjects: @"py", @"pyc", @"pyo", nil]; NSString *mainFilePath = nil; for (NSString *possibleMainExtension in possibleMainExtensions) { mainFilePath = [mainBundle pathForResource: @"main" ofType: possibleMainExtension]; if ( mainFilePath != nil ) break; } if ( !mainFilePath ) { [NSException raise: NSInternalInconsistencyException format: @"%s:%d main() Failed to find the Main.{py,pyc,pyo} file in the application wrapper's Resources directory.", __FILE__, __LINE__]; } Py_SetProgramName("/usr/bin/python"); Py_Initialize(); const char *mainFilePathPtr = [mainFilePath UTF8String]; FILE *mainFile = fopen(mainFilePathPtr, "r"); int result = PyRun_SimpleFile(mainFile, (char *)[[mainFilePath lastPathComponent] UTF8String]); if ( result != 0 ) [NSException raise: NSInternalInconsistencyException format: @"%s:%d main() PyRun_SimpleFile failed with file '%@'. See console for errors.", __FILE__, __LINE__, mainFilePath]; [self willChangeValueForKey: @"arrayOfNamedStrings"]; [self didChangeValueForKey: @"arrayOfNamedStrings"]; } - (NSArray *) arrayOfNamedStrings; { if (!stuff) { Class PythonStuffClass = NSClassFromString(@"PythonStuff"); stuff = [PythonStuffClass new]; } return [stuff arrayOfNamedStrings]; } @end