【问题标题】:How to run a command in terminal from my Cocoa app?如何从我的 Cocoa 应用程序在终端中运行命令?
【发布时间】:2015-01-01 02:30:02
【问题描述】:

我想通过我的 cocoa 应用使用 instrument 来在我的 iOS 模拟器上安装 .app。

这是我第一次开发可可应用程序和 NSTask。 NSTask 需要一个启动路径,在我的情况下它是无关紧要的,因为这个命令可以从任何地方运行。 这是我要运行的命令:

instruments -t /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.xrplugin/Contents/Resources/Automation.tracetemplate -w "iPad Retina (7.1 Simulator)" ""

目前我有这个:

NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath:@"/"];

    NSString * commandTorun = @"instruments -t /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.xrplugin/Contents/Resources/Automation.tracetemplate  -w \"iPad Retina (7.1 Simulator)\" \"<path to .app>" ";

    NSArray *arguments = [NSArray arrayWithObjects:
                          @"-t" ,
                          [NSString stringWithFormat:@"%@", commandTorun],
                          nil];

    NSLog(@"Run command:%@",commandTorun);
[task setArguments:arguments];
[task launch];

任何帮助将不胜感激。

谢谢,

【问题讨论】:

    标签: objective-c xcode cocoa instruments nstask


    【解决方案1】:

    我们开始吧。

    工作解决方案。 XCode 7.3 iPhone 6 (9.3)

     NSTask *task = [[NSTask alloc] init];
        [task setLaunchPath: @"/Applications/Xcode 7.3.app/Contents/Developer/usr/bin/instruments"];
        NSArray *arguments = [NSArray arrayWithObjects:
                              @"-t",
                              @"/Applications/Xcode 7.3.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.xrplugin/Contents/Resources/Automation.tracetemplate",
                              @"-w",
                              @"iPhone 6 (9.3)",
                              @"/Users/ab56353/Desktop/FirstTest.app",
                              @"-e",
                              @"UIASCRIPT",
                              @"/Users/ab56353/Desktop/TapGeneration.js",
                              nil];
        [task setArguments:arguments];
        [task launch];
    

    注意:放置正确的 xcode 应用名称 (/Applications/xcode 7.3) 和模拟器或设备 udid(如果是物理设备)。

    【讨论】:

      【解决方案2】:

      启动路径是命令的路径,您似乎将命令名称放在参数中的“-t”之后。试试这样的:

      NSTask *task = [[NSTask alloc] init];
      [task setLaunchPath: @"/Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/MacOS/Instruments"];
      NSArray *arguments = [NSArray arrayWithObjects:
                      @"-t",
                      @"/Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.xrplugin/Contents/Resources/Automation.tracetemplate"
                      @"-w",
                      @"iPad Retina (7.1 Simulator)",
                      nil];
      [task setArguments:arguments];
      [task launch];
      

      我认为 NSTask 会在参数周围放置引号,但我不确定,因此您可能需要将转义引号放回“iPad...”参数周围。

      【讨论】:

      • 不幸的是它不一样。在终端中运行仪器命令不同于 /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/MacOS/Instruments"
      【解决方案3】:

      stdlib 中的系统函数会做你想做的事。

      只需将整个命令作为参数调用它:

      system("instruments -t /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.xrplugin/Contents/Resources/Automation.tracetemplate  -w \"iPad Retina (7.1 Simulator)\" \"<path to .app>" ")
      

      【讨论】:

        猜你喜欢
        • 2010-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多