Run AppleScript from Cocoa Application

Fábio Perez picture Fábio Perez · Feb 5, 2011 · Viewed 16.7k times · Source

Is it possible to run an AppleScript code inside an Cocoa Application?

I've tried NSAppleScript class, but no success.

Also, does Apple allow this?

Answer

Fábio Perez picture Fábio Perez · Feb 5, 2011

Solved!

Xcode wasn't saving my script file into app's resources path. To run an AppleScript code from Cocoa Application, use this:

NSString* path = [[NSBundle mainBundle] pathForResource:@"ScriptName" ofType:@"scpt"];
NSURL* url = [NSURL fileURLWithPath:path];NSDictionary* errors = [NSDictionary dictionary];
NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
[appleScript executeAndReturnError:nil];
[appleScript release];