« Where are we standing | Main | WWDC, the trip »

March 31, 2006

Executing an Applescript from Cocoa

So I spent some time digging around and couldn't find much about cocoa+applescript except for examples of how to put the code in the same class as the cocoa. So after looking around, pestering some folks, and coming up with some of my own stuff, this is what I came up with:



#import "RunScript.h"

#define runScriptName @"checknewnow"
#define runScriptType @"scpt"


@implementation RunScript

- (IBAction)runScript:(id)sender
{
/* Locate that darn thing*/
NSString *scriptPath = [[NSBundle mainBundle] pathForResource: runScriptName ofType: runScriptType];
NSURL *scriptURL = [NSURL fileURLWithPath: scriptPath];

//NSLog(@"%@", scriptURL);

NSAppleScript *as = [[NSAppleScript alloc] initWithContentsOfURL:scriptURL error:nil];
[as executeAndReturnError:nil];
//NSLog(@"foomx");
}

@end

Thanks to David Smith and Andrew Wellington for their help.

Posted by Chris Forsythe at March 31, 2006 11:01 PM

Comments

In the call to -executeAndReturnError:, nil should be NULL (because you're passing a pointer, not an instance).

Posted by: Mac-arena the Bored Zo at March 31, 2006 11:25 PM

If you want to run a paramterised script - you may want to check out this:

http://weblog.scifihifi.com/2004/04/15/calling-applescript-handlers-from-cocoa

Posted by: Diggory Laycock at April 1, 2006 12:37 PM