Search code examples
objective-cxcodecocoaapplescriptnsbutton

Run an AppleScript from project when NSButton is clicked


I have an AppleScript that I have imported into my Xcode project correctly and I have also set up a -(IBAction) to handle when the user clicks the button however I am unable how to run an AppleScript when the button is clicked.

I would essentially like the script to run just as it would if it were opened via the Finder.


Solution

  • NSString* path = [[NSBundle mainBundle] pathForResource:@"finder" ofType:@"scpt"];
    NSURL* url = [NSURL fileURLWithPath:path];
    NSAppleScript* appleScript =[[NSAppleScript alloc] initWithContentsOfURL:url error:nil];
    [appleScript executeAndReturnError:nil];