• 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: has <email@hidden>
  • Date: Fri, 4 Jan 2008 22:46:36 +0000

email@hidden 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: Printing a layer-backed view
  • Next by Date: Re: Image scaling
  • Previous by thread: Accessing script run handler from NSAppleScript
  • Next by thread: Re: Accessing script run handler from NSAppleScript
  • Index(es):
    • Date
    • Thread