Objective-Everything Release 5. Copyright ©1994-1998 by TipTop Software, Inc. All Rights Reserved.
In ObjShell: Double-click on ObjShell.app. Click "Load Python", and then click "Python". In a terminal shell, type objpy. To run the application kit type:
import AppKit AppKit.run()
To open an Interactor window, click Objective->Interact->Interactor, and select "Python". [This does not work in Windows, due to Windows limitations. In Windows, you have to run ObjShell.app.]
In InterfaceBuilder: Load ObjPalette.palette from /Local/Developer/Palettes/. Click Tools->Objective->Interactor->Interact and select Python to open an ObjPython interactor window.
All of the following examples are executed in InterfaceBuilder.
All of the following examples also assume that all symbols from the ObjPy module are imported in the current namespace:
from ObjPy import *
C functions live in the C namespace. E.g., in an Interactor window, type:
C.NSRunAlertPanel("Python","Hello World!",nil,nil,nil)
To read a value, "call" it by name, or access it as a Python global variable:
py% print 'argc: %d, argv: %s' % (CGlob.NSArgc,CGlob.NSArgv.val) argc: 1, argv: ('/NextDeveloper/Apps/InterfaceBuilder.app/InterfaceBuilder',) # Define global NSGenericException, and then access it # (it is a string with contents "NSGenericException") py% C.NSGenericException=C(Type('NSString*'),'NSGenericException') py% print C.NSGenericException.val NSGenericException
Use Python syntax to send a message.
py% CGlob.NSApp.clazz() Class.IB py% CGlob.NSApp.showInfoPanel(nil) # Construct an object: # Class.NSObject() is equivalent to Class.NSObject.alloc().init().autorelease(). py% x=Class.NSObject() # ObjPy automatically takes care of memory management. # In this example, although the object is (auto)released, it is not yet deallocated, # since the Python variable x holds a reference to the object. py% x <NSObject: 0x...> py% x.description() '<NSObject: 0x...>' py% x=None # The object is now really gone. # ObjPy also detects when objects become illegal. py% x=Class.NSObject() py% x.release() py% x.description() Traceback (innermost last): File "<string>", line 1, in ? ObjPy Runtime Error: illegal target: Dealloc'ed object
Define a category on a class.
py% def sayHelloTo(self,name): %-> """-(void)sayHelloTo:(NSString*)name""" %-> C.NSRunAlertPanel('Python','Hello %s' % name, nil,nil,nil) %-> py% Class.NSObject.category([sayHelloTo]) py% CGlob.NSApp.sayHelloTo(C.NSFullUserName())
You can use "extend" as a synonym for "category".
Classes are defined using the "class" command. Instance variables and method prototypes are specified using the ObjC syntax.
py% Class.NSObject.subclass('MyObject', """ %-> NSString *aString; %-> id anObject; %-> char *aCString; %-> """) Class.MyObject py% py% def dealloc(self): %-> """-(void)dealloc""" %-> print self,',',_cmd %-> self['aString']=nil %-> self['anObject']=nil %-> self['aCString']=None %-> super.dealloc() %-> py% def setStringValue(self,_cmd,s): %-> """-(void)setStringValue:(NSString*)s""" %-> print self,',',_cmd,',',s %-> self['aString']=s %-> py% def stringValue(self,_cmd): %-> """-(NSString*)stringValue""" %-> print self,',',_cmd %-> return self['aString'] %-> py% # etc.
py% Class.MyObject.extend([dealloc,setStringValue,stringValue])
py% obj=Class.MyObject() py% obj.setStringValue('Hello World!') <MyObject: 0x...>,setStringValue:,Hello World! py% obj.stringValue() <MyObject: 0x...>,stringValue 'Hello World!' py% C(obj).val (Class.MyObject, 'Hello World!', nil, None) py% del obj <MyObject: 0x...>,dealloc
Objective-Everything lets you define instance-specific instance methods.
py% x=C.MyObject() py% y=MyObject() py% def sayHello(self): %-> """-(const char*)sayHello""" %-> return "Hello from %s" % self %-> py% x.extend([sayHello]) py% x.sayHello() 'Hello from @MyObject: 0x...>' py% y.sayHello() Traceback (innermost last): File "<string>", line 1, in ? ObjPy Runtime Error: <MyObject:0x...> does not respond to sayHello ... py% x.clazz() Class.MyObject py% y.clazz() Class.MyObject
# Override -showInfoPanel: of CGlob.NSApp. # First, find out what the -showInfoPanel: prototype is py% print unparse(CGlob.NSApp) @interface IB : NSApplication <IB_Full> // Subclasses: None { ... }
... - (void)showInfoPanel:(id)a0; ... @end
# Or, simply: py% Info.methods(CGlob.NSApp.clazz(),'showInfo*') ('- (void)showInfoPanel:(id)a0;',)
# Now, override -showInfoPanel: to run an alert panel, and then invoke # the previous (in this case ObjC) method implementation. py% def showInfoPanel(self,sender): %-> """-(void)showInfoPanel:(id)sender""" %-> C.NSRunAlertPanel('Python','About to show IB info panel',nil,nil,nil) %-> ex.showInfoPanel(sender) %-> py% CGlob.NSApp.extend([showInfoPanel])
Now, click Info->InfoPanel.
Click Document->NewApplication. Drag the ObjPy interpreter object from ObjPy palette into the object suitcase. Drag a button into the window. Ctrl-connect the button to the interpreter object, and select the newWInteractor: action.
Now, when you run the nib file by clicking Document->TestInterface, when you click the button, you will get an interactor window in which you can type commands to be executed by the interpreter.
Add another button into the window. Ctrl-drag from the interpreter instance to this button. Type "but" in the connections inspector. Ctrl-drag from the interpreter instance to the "My Window" window title bar. Type "win".
Run the interface (Cmd-r or click Document->TestInterface). Hit the button which opens the interactor window. Type:
py% but.setTitle('Hello') py% win.setTitle('World')
Quit the TestInterface mode. Close the nib file (save it if you like).
In this example we build a simple expression calculator which simply uses the Python eval command to evaluate mathematical expressions. Click Document->NewApplication. Drag the TextField object into window "My Window". Change it to look something like:
Drag the NibImplementation object from ObjPalette into the object suitcase. Ctrl-Alt-drag from the code object to the result field. Select "resultField".
In the implementation inspector, select Py. The code object will then use the Python interpreter for evaluation. Hit "+" and implement the action object as follows:
import ObjPy; from ObjPy import * class __Extension__: def action(self,_cmd,sender): """-(void)action:(id)sender;""" r=eval(sender.stringValue()) self.__xv__.resultField.setStringValue(r) self.extend(__Extension__)
Ctrl-drag from the "Expr:" field to the code object, and
connect it to the "action:" action.
Test the interface: Type "1+2" and hit Return.
Close the nib file (save it if you like).
The Code object lets you implement interpreted action methods without having to implement a new class. In general, the Code object is useful for implementing simple actions.
Another way of implementing actions is to directly extend an instance with an action method. In this example we implement the same calculator as in the previous example. Create a window with the fields, like in the previous example.
Drag the ObjPy interpreter from the ObjPy palette into the object suitcase. Connect fields as globals in the interpreter with names "rfield" and "efield" (Ctrl-drag from the interpreter to each field, and type the field name).
Since we want to define an action for "efield", double-click on the "efield" in the ConnectionInspector. Click "Create" when you are asked whether to create an action implementation.
Implement the action as follows:
from math import * import sys
### efield ### def action(self,sender): """-(void)action:(id)sender""" rfield.setStringValue(eval(self.stringValue())) efield.extend([action])
Run and test the interface.
Now, add some error handling; change action to:
def action(self,sender): """-(void)action:(id)sender""" try: r=eval(self.stringValue()) except: r=str(sys.exc_value) C.NSRunAlertPanel('Error',r,nil,nil,nil) rfield.setStringValue(r)
Note: You should not define a class in a NIB file (in interpreter startup code, or in a code object) if the NIB file contains an instance of that class, because it is not possible to ensure that the code is executed before the instance is instantiated.
Yet another way of implementing actions is to directly extend an instance with an action method. In this example we implement the same calculator as in the previous example.
Create a window with the fields, like in the previous example.
Double-click on the "Expr:" field to select it. Connect xvar "resultField" of the "Expr:" field to the "Result:" field: Ctrl-Alt-Drag from the "Expr:" field to the "Result:" field, type "resultField", and hit Return.
Since we want to define an action for the "Expr:" field, double-click on the "Expr:" field to select it. In the Implementation inspector select "Py", hit "+", and implement the action as follows:
import ObjPy; from ObjPy import * class __Extension__: def action(self,_cmd,sender): """-(void)action:(id)sender;""" r=eval(self.stringValue()) self.__xv__.resultField.setStringValue(r) self.extend(__Extension__)
Run and test the interface.
In a terminal shell, type:
robjpysh IBInterp
This will give you access to the ObjPy interpreter in InterfaceBuilder. You can now issue any command to this interpreter; e.g., type: CGlob.NSApp.showInfoPanel(nil)
To set a remote interpreter connection in InterfaceBuilder: Drag an Interactor object from the ObjPalette into the object suitcase. Drag a button into the window. Connect a button to the interactor, so that it sends the "run:" message. In the attribute inspector for the interactor, click the Remote switch, so that it is ON, enter "IBInterp" into the connection field, enter "ObjPyInterp" into the interpreter field, and hit return. Now when you run the nib file, and hit the button, the interactor will connect to IB (itself)...
Type "AppKit.browse(CGlob.NSApp)" in an interactor window, and browse away!
To run browser from a Terminal shell:
import AppKit AppKit.run(command=AppKit.browse)
Use ProjectBuilder to create an application project, and use InterfaceBuilder to create nib files. Make sure that you link the application with ObjCore and ObjAppKit frameworks. When you build and run the application, Objective-Everything will get initialized...
Note: Under Windows, you need to reference a symbol in each of the frameworks that you link with. E.g., in YourApp_main.m, make sure you do:
#import <ObjAppKit/ObjAppKit.h>
int main(int argc, const char *argv[]) { { [TTInterp class]; [TTApplication class]; return NSApplicationMain(argc, argv); }
See Using PB for how to include ObjPy files in the project, how to manage and edit them using PB.
If you have a NIB file which you want to run as an application, you can simply run objpysh, and type:
AppKit.run(nib='/your/nib/file.nib')
Statically link your application with the ObjCore and ObjAppKit frameworks; or, dynamically load these frameworks:
[[NSBundle bundleWithPath:@"/Local/Library/Frameworks/ObjAppKit.framework"] principalClass];
Then, at runtime, to open an interactor window:
[TTInterp newWInteractor:nil];
or, send the newWInteractor: message to the first responder, e.g., via a button, or a menu item. This will give you a starting point for interactively issuing commands to your app.
For example, to run Objective-Browser inside EOModeler:
objmeddle /System/Developer/Applications/EOModeller.app/EOModeler
You can now run an interactor from the Objective menu (in Tools), or run ObjBrowser, etc.
Read the documents in ObjCore/Documentation/Concepts to become familiar with the general Objective-Everything concepts. Read other documents in this directory to get familiar with ObjPy.
Also, make sure that you look at the examples which are included in each Obj<Lang>.framework/Resources/Examples directory.