2

I have a small applescript and I want to run it as soon as a button is pressed in my application, how do I get this done? I've made this OS X app in Objective-C.

Rakshith G B
  • 816
  • 2
  • 8
  • 24

1 Answers1

2

NSAppleScript can compile and run AppleScripts. For example if your script is in the folder Scripts in the Resources folder

NSString *compiledScriptPath = [[NSBundle mainBundle] pathForResource:@"myScript" ofType:@"scpt" inDirectory:@"Scripts"];
NSDictionary *error = nil;
NSAppleScript *script = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:compiledScriptPath] error:&error];
if (error) {
    NSLog(@"compile error: %@", error);
} else {
   [script executeAndReturnError:&error];
   if (error) {
     NSLog(@"run error: %@", error);
   }
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • NSAppleScript can take complied scripts (`initWithContentsOfURL`) as well as just literal source text (`initWithSource`) – vadian Aug 01 '15 at 11:59