Skip navigation.
 
mlRe: Accessing script run handler from NSAppleScript
FROM : has
DATE : Fri Jan 04 23:46:36 2008

<email_removed> wrote:

> I am trying to call as wide as possible a variety of AppleScripts 
> from cocoa using NSAppleScript and am having trouble
> finding a way of calling a script with a parameterised run handler.
> [...]


> Calling the run handler explicitly by name with NSAppleScript - 
> executeAppleEvent results in:
>
> Error -1708 occured the run(Hello) call: (null)


'run' is an AppleScript-defined keyword, not a user-defined 
identifier, so make sure you're using the appropriate AE codes 
('aevt'/'oapp' in this case). Example:

#import "Appscript/Appscript.h"


int main (int argc, const char * argv[]) {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   // Create AEMApplication object for building AEMEvents
   AEMApplication *app = [[AEMApplication alloc] init];

   // Create AEMCodecs object for unpacking script results
   AEMCodecs *codecs = [[AEMCodecs alloc] init];

   // Build parameter list for script
   NSArray *params = [NSArray arrayWithObjects:@"Hello ", @"World!", nil];

   // Create 'aevtoapp' (AppleScript subroutine) AEMEvent
   AEMEvent *evt = [app eventWithEventClass:kCoreEventClass
                                    eventID:kAEOpenApplication];

   // Add parameter list to AEMEvent
   [evt setParameter:params forKeyword:keyDirectObject];

   // Compile (or load) the AppleScript
   NSAppleScript *scpt = [[NSAppleScript alloc] initWithSource:
           @"on run {arg1, arg2}\n"
           @"    return arg1 & arg2\n"
           @"end run"];

   // Get an NSAppleEventDescriptor from AEMEvent
   NSAppleEventDescriptor *evtDesc = [evt appleEventDescriptor];

   // Send event to AppleScript
   NSDictionary *error;
   NSAppleEventDescriptor *resDesc = [scpt executeAppleEvent:evtDesc 
error:&error];

   // Unpack script result
   id res = [codecs unpack:resDesc];
   NSLog(@"Result = %@", res); // "Result = Hello World!"

   [scpt release];
   [codecs release];
   [app release];
   [pool release];
   return 0;
}


Notes:
- An aevt/oapp event will work even for scripts that don't have 
explicit run handlers.

- While you must pass all parameters expected by the script, any 
excess parameters are simply ignored.  That means you can always pass 
parameters, and if the script doesn't need them it can simply ignore 
them.

has
--
http://appscript.sourceforge.net
http://rb-appscript.rubyforge.org

Related mailsAuthorDate
mlAccessing script run handler from NSAppleScript jonathan@mugginsof… Jan 4, 21:56
mlRe: Accessing script run handler from NSAppleScript has Jan 4, 23:46
mlRe: Accessing script run handler from NSAppleScript jonathan@mugginsof… Jan 5, 01:02