How do I add Applescript support to my Cocoa application?

后端 未结 3 1232
感动是毒
感动是毒 2020-12-22 19:20

I am new to the world of Cocoa programming, and I want to add Applescript support to my application. The examples at Apple\'s website seem out of date.

How do I add

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-22 19:21

    A simple example to get you started,

    place a script (named dialog) into the documents folder then you can run it from Xcode

    NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDirectory = [arrayPaths objectAtIndex:0];
        NSString *filePath = [docDirectory stringByAppendingString:@"/dialog.scpt"];
    
         NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath] error:nil];
    
     [scriptObject executeAndReturnError:nil];
    

    The nice thing about keeping the script external is the ability to edit it outside of Xcode. I would recommend adding the error checking if you did start editing as the applescript may not compile

    maybe check with

    if(scriptObject.isCompiled){
    

提交回复
热议问题