Call instance method woes

Hi everyone,

I’m trying to call an instance method from AppleScript Studio. I read and I thought I could just steal StefanK’s sharedInstance but I can’t get that to work…

This is my AppleScript code:

    set sharedInstance to call method "sharedInstance" of class "AppController"
    call method "flushVideoInformation" of sharedInstance


[code]#import <Cocoa/Cocoa.h>

@interface AppController : NSObject {
IBOutlet NSTextField *videoHeader;

  • sharedInstance;
  • (void)flushVideoInformation;

[code]#import “AppController.h”

@implementation AppController

  • sharedInstance
    static id sharedTask = nil;
    if(sharedTask == nil) {
    sharedTask = [[[self alloc] init] autorelease];
    return sharedTask;
  • (void)flushVideoInformation {
    [videoHeader setStringValue:@“”];
    I know you’re all thinking like, “Hey, you can just do that in AppleScript Studio without Objective-C!”, but this is just an example, the original code is larger.

My Error : “No result was returned from some part of this expression.” (-2763)

Thanks in advance.


I guess you have to implement the init method, which initializes the instance calling the super class.
It’s not necessary to declare the method in the .h file

- (id)init { self = [super init]; return self; }

Thanks mate ! :smiley:
I had a suspicion it had something to do with that but since it was an instance itself I didn’t include it.

Thanks again!

every instance is a subset of a class, and every class needs the init method if a custom initializer calls [self . init]

One more question, is it normal that I can’t access my IBOutlets through the AppleScript call anymore ?

I don’t know, I always pass the outlets as parameters when I call custom classes from AppleScript

I created an instance just to access those IBOutlets and that I didn’t had to pass them from AppleScript. I though ObjC would have had something like in AppleScript text field 1 of window 1. You know, the path. But as far as I know it hasn’t and I wanted to use IBOutlets so I didn’t have to pass all the parameters.

The code gets executed but the value is b[/b]

NSString *i = [videoFormat title]; NSLog(@"i: %@", i);
Thanks anyway :smiley:

maybe you use the wrong message. NSTextField has no title method

NSString *i = [videoFormat stringValue]; NSLog(@"i: %@", i);

No, I first tried NSTextField → stringValue and than I tried NSButton → title… Both gave the same (null) result

Did you make all connections in Interface Builder?

Create object (blue cube)
Type the name of your class into the identity field
Connect the outlet to the UI element

Yes. And the connection also appears in the “Connections” tab of IB (the blue thing with the arrow).

I tested it (on Leopard) with a sample project, same behavior. It does not work
A workaround is to write set accessors for the UI elements like

- (void)assignTextField:(NSTextField *)f { field = f; }
and pass the references after initializing the instance

set instance to call method "sharedInstance" of class "myClass"
    call method "assignTextField:" of instance with parameter (a reference to text field "textfield" of window "main")

Thanks for taking your time looking into it.

Do you think I can’t access them because it’s how ObjC works or because I use AppleScript Studio to first create the instance ?

I really hoped that ObjC had something like

text field "textField" of window "main"

But it seems I either have to use IBOutlets or pass them from AppleScript. That makes me think, since Apple’s apps aren’t written in AppleScript, they can’t pass them from it so they have to use ObjC. So, in an instance they access the element somehow different. That’s why I thought something like the script above would exist in ObjC

Thanks :smiley:

I really don’t know. Actually creating an instance should provide access to the IBOutlets defined in the ObjC class.

So normally without any AppleScript, pure ObjC, creating an instance does work with IBOutlets ?

Of course :slight_smile:

I have an idea, what if I call sharedInstance from within ObjC (from i.e. awakeFromNib) maybe it will work then. I don’t know how to use awakeFromNib in ObjC but I’ll check on it tomorrow. Can’t be too hard…

I fail to call it from awakeFromNib :expressionless: