1. 程式人生 > >Mac 命令列安裝Chrome外掛

Mac 命令列安裝Chrome外掛

- (void)startInstallChromeExtension {
    NSURL *url = [[NSWorkspace sharedWorkspace] URLForApplicationWithBundleIdentifier:@"com.google.Chrome"];
    if (url) {
        NSString *chromeExtensionPath = [[NSBundle mainBundle] pathForResource:EXTENSIONSOURCENAME ofType:@"crx" inDirectory:EXTENSIONDIRNAME];
        NSDictionary *configuration = [NSDictionary dictionaryWithObjectsAndKeys:
                                       [NSArray arrayWithObjects:@"--enable-easy-off-store-extension-install",chromeExtensionPath,nil],NSWorkspaceLaunchConfigurationArguments, nil];
        
        [[NSWorkspace sharedWorkspace] launchApplicationAtURL:url
                                                      options:NSWorkspaceLaunchDefault
                                                configuration:configuration error:nil];
        
    }
}

- (IBAction)installChromeExtension:(id)sender {
    NSArray *runningChromes = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.google.Chrome"];
    if ([runningChromes count] > 0) {
        NSAlert * alert = [NSAlert alertWithMessageText:NSLocalizedString(@"安裝Chrome外掛,需要關閉正在執行的Chrome瀏覽器,是否允許?", @"")
                                          defaultButton:NSLocalizedString(@"OK", @"")
                                        alternateButton:NSLocalizedString(@"Cancel", @"")
                                            otherButton:nil
                              informativeTextWithFormat:@""];
        
        NSInteger result = [alert runModal];
        
        if (result == NSAlertDefaultReturn ) {
            for (int i = 0; i < [runningChromes count]; i++) {
                [(NSRunningApplication *)[runningChromes objectAtIndex:i] forceTerminate];
            }
            [self performSelector:@selector(startInstallChromeExtension) withObject:nil afterDelay:0.5];
        }
    }
    else {
        [self performSelector:@selector(startInstallChromeExtension) withObject:nil afterDelay:0.5];
    }
}