How to share classes instances between Os X App and appleScript cmd

I have validated my app as scriptable and constructed the suite within a.sdef file, and when I send a command through appleScript editor, I reach the right class (for me SolarMaxAppleEvent class) and process performDefaultImplementation method, which returns a NSString.

This class has been intanciated by my app which fills in an array (NSMutableArray * AppleEventUpsArray) of Dictionaries (NSMutableDictionary * appleEventUpsDict) which I want to use in AppleScript Answer.

In order to fill in this string, I need to acces to this AppleEventUpsArray property in performDefaultImplementation method.

But running inside this method (with debug) it seems, that I cannot access to the same instance than in the one filled by the app.

How can I access within performDefaultImplementation method to the properties of this class?

Sample code follows:

file: SolarMaxAppleEvent.h

@interface SolarMaxAppleEvent : NSScriptCommand {

@public NSMutableArray *AppleEventUpsArray;

@public NSMutableDictionary *appleEventUpsDict;

}

  • (id)performDefaultImplementation;

@property (nonatomic, retain) NSMutableArray *AppleEventUpsArray;

@property (nonatomic, retain) NSMutableDictionary *appleEventUpsDict;

@end

file: SolarMaxAppleEvent.m

@implementation SolarMaxAppleEvent

@synthesize AppleEventUpsArray;

@synthesize appleEventUpsDict;

  • (id)init {

    int i;

    if (self = [super init]) {

    // NSLog(@"SolarMaxAppleEvent init ");

    self.AppleEventUpsArray = [[NSMutableArray alloc] init];
    
      for (i=0 ; i< 3 ; i++) {
    
          appleEventDict = [[NSMutableDictionary alloc] init];
    
          appleEventDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
    
              [NSDate date], @"Date", [NSNumber numberWithInteger: 0 ], @"numPort",
    
              [NSNumber numberWithInteger: 0 ],@"UAC" ,
    
              [NSNumber numberWithInteger: 0 ],@"PAC" ,
    
              [NSNumber numberWithInteger: 0 ],@"Temp",
    
              [NSNumber numberWithInteger: 0 ],@"UDC" ,nil ];
    
          [self.AppleEventUpsArray addObject: appleEventDict];
    
          [appleEventDict release];
    
        }
    
      NSLog(@"SolarMaxAppleEvent init AppleEventUpsArray : %@ ",AppleEventUpsArray);
    

// at this point This AppleEventUpsArray is correctly filled with 3 objects (when seen in apple Debugger)

}

return self;

}

// Following request from apple script, program enters this following method.

  • (id) performDefaultImplementation {

    id currentEventArray;

    NSDictionary * theArguments = [self evaluatedArguments];

    NSString *theResult;

    SLOG(@“CommandWithArgs performDefaultImplementation”);

    /* report the parameters */

    SLOG(@“The direct parameter is: ‘%@’”, [self directParameter]);

    SLOG(@“The other parameters are: ‘%@’”, theArguments); // I clearly see this Log within console with the rignt arguments

      currentEventArray = self.AppleEventUpsArray; 
    
      theResult = [NSString stringWithFormat:@"'%@'", [self.currentEventArray count]]; 
    

// which returns 0 to apple script command instead of 3 and debugger indicates that self.AppleEventUpsArray pointer is 0

    SLOG (@"Result Sent %@" ,theResult);

return theResult;

}


// Question: How can I access to AppleEventUpsArray within performDefaultImplementation?

// Which bind operation, or class declaration (in sdef file) must I declare to share the same values between apple script command and X code application ?
Thanks for your answer.

Hi,

the init method of the NSScriptCommand subclass is never called. You have to build the result in the performDefaultImplementation method

And change the format placeholder in this line to %lu

theResult = [NSString stringWithFormat:@"'%lu'", [self.currentEventArray count]]; 

Thanks
But when application is launched via applescript tell command, init method of the NSScriptCommand subclass should be called (because this class has been initialized by application code) ? is’nt it ?
if yes, AppleEventUpsArray should not be null pointer ?
Let me know
Thanks

Model: powerbook
Browser: Firefox 26.0
Operating System: Mac OS X (10.8)

Fix the format string as Stefan indicated; passing an integer for %@ is likely to cause all sorts of problems.

(I’m not sure why you’re doing the work in init; if you do it in performDefaultImplementation you can dispense with the property.)