美文网首页
八、AppleScript与OC交互

八、AppleScript与OC交互

作者: 快乐的老船长 | 来源:发表于2019-01-27 22:52 被阅读475次

    依旧假使有一个需求,很简单,根据xcodeproj路径打开该Xcode工程。若该工程已经打开,则关闭后再打开。

    如果是通过OC来实现,OC可以调用下面的方法来实现打开Xcode工程。

    [[NSWorkspace sharedWorkspace] openFile:@"对应的xcodeproj路径" withApplication:@"该Mac的Xcode名称" andDeactivate:YES];
    

    但是没有办法重新打开该工程呀,在我已知的方法里OC没有办法来操作关闭Xcode这样的需求。那么来通过AppleScript来操作吧。AppleScript本身就是来操作Mac上的应用程序的语言。

    我们先通过脚本编辑器来尝试下如何去关闭Xcode。

    首先打开 脚本编辑器> 文件 > 打开词典 来查看Xcode的支持的AppleScript的操作有哪些?

    image.png

    我这里有三个Xcode,应用程序的名字不一样,但是这几个关闭和打开Xcode的AppleScript的命令都是一样的。随便选择一个


    WeChatd65df5f7249ffb7c87f00b4560eacc85.png

    看到open方法,可以打开对应的文件。我们调用试下

    tell application "Xcode8.1" -- 填写需要打开的应用名称
        open "/Users/lujh/Test/Test.xcodeproj"
    end tell
    

    能够成功打开。
    接下来close,close可以关闭文件或者windows。

    tell application "Xcode"
            
            repeat with aWindow in windows
                
                set aDocument to document of aWindow
                set currentPath to ""
                try
                    set currentPath to path of aDocument
                end try
                if currentPath = "/Users/lujh/Test/Test.xcodeproj" then
                    close aWindow
                    exit repeat
                end if
                
            end repeat
        end tell
    

    遍历所有已经打开的Xcode的windows,判断path与要关闭的文件路径是否一致,一致则关闭该window。

    现在我们已经能够满足关闭和打开该工程了。那么整合下,给出一个便于调用的接口,handler。需要两个参数,Xcode的名称和需要重新打开的xcodeproj文件的路径。

    AppleScript代码

    on reopenWindowWithPath(XcodeName, windowPath)
        
        tell application XcodeName
            
            repeat with aWindow in windows
                
                set aDocument to document of aWindow
                set currentPath to ""
                try
                    set currentPath to path of aDocument
                end try
                if currentPath = windowPath then
                    
                    close aWindow
                    exit repeat
                    
                end if
                
            end repeat
            
            open windowPath
            
        end tell
        
        return
        
    end reopenWindowWithPath
    

    在脚本编辑器,将文件保存为scpt格式。只有这个格式才能被OC调用。

    通过OC调用AppleScript,创建NSAppleScript对象,传入AppleScript所在的url。创建NSAppleEventDescriptor对象,指定suite、eventID等,这几个宏是固定的,因为我们不需要其他的功能,所有不导入其他的库,只重新声明下这几个宏定义。然后传入指定的handler的函数名,key是固定的‘snam’,并通过同样的方法传入需要的参数。最后调用executeAppleEvent 执行AppleScript。

    OC 代码

    + (void)reopenXcodeWindowWithXcodeName:(NSString *)xcodeName projectPath:(NSString *)path
    {
        NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"MobSDKToolScript" ofType:@"bundle"];
        NSBundle *scriptBundle = [NSBundle bundleWithPath:bundlePath];
        NSURL *url = [scriptBundle URLForResource:@"reopenXcode" withExtension:@"scptd" subdirectory:@"AppleScript"];
        
        NSAppleScript           * appleScript;
        NSAppleEventDescriptor  * thisApplication, *containerEvent;
        
        NSDictionary * appleScriptCreationError = nil;
        appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&appleScriptCreationError];
        
    #define kASAppleScriptSuite 'ascr'
    #define kASSubroutineEvent  'psbr'
    #define keyASSubroutineName 'snam'
        containerEvent = [NSAppleEventDescriptor appleEventWithEventClass:kASAppleScriptSuite
                                                                  eventID:kASSubroutineEvent
                                                         targetDescriptor:thisApplication
                                                                 returnID:kAutoGenerateReturnID
                                                            transactionID:kAnyTransactionID];
        
        [containerEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithString:@"reopenWindowWithPath"]
                                forKeyword:keyASSubroutineName];
        
        NSAppleEventDescriptor  *arguments = [[NSAppleEventDescriptor alloc] initListDescriptor];
        NSString                *object;
        
        NSArray *scriptArgumentArray = @[[xcodeName stringByDeletingPathExtension], path];
        
        for (object in scriptArgumentArray)
        {
            [arguments insertDescriptor:[NSAppleEventDescriptor descriptorWithString:object]
                                atIndex:([arguments numberOfItems] + 1)];
        }
        
        [containerEvent setParamDescriptor:arguments forKeyword:keyDirectObject];
        
        //Execute the event
        NSDictionary *executionError = nil;
        NSAppleEventDescriptor *result = [appleScript executeAppleEvent:containerEvent error:&executionError];
        if (executionError != nil)
        {
            DLog(@"%@", [NSString stringWithFormat:@"error while executing script. Error %@",executionError]);
            
        }
        else
        {
            DLog(@"Script execution has succeed. Result(%@)",result);
        }
    }
    

    相关文章

      网友评论

          本文标题:八、AppleScript与OC交互

          本文链接:https://www.haomeiwen.com/subject/kvunjqtx.html