• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Accessing script run handler from NSAppleScript
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accessing script run handler from NSAppleScript


  • Subject: Re: Accessing script run handler from NSAppleScript
  • From: "email@hidden" <email@hidden>
  • Date: Sat, 5 Jan 2008 00:02:44 +0000

Great reply. Googling for AEMApplication shows that you have more or less answered my question on the list before.
Apologies that I didn't find it.


Many thanks

Jonathan


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

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: Image scaling
  • Next by Date: Sending an event from another thread
  • Previous by thread: Re: Accessing script run handler from NSAppleScript
  • Next by thread: iChat-Theater not working from Cocoa apps
  • Index(es):
    • Date
    • Thread