How to run a command in terminal from my Cocoa app?

杀马特。学长 韩版系。学妹 提交于 2020-01-02 09:55:45

问题


I want to use instrument to install the .app on my iOS simulator through my cocoa app.

This is my first time developing a cocoa app and also NSTask. NSTask requires a launch path which in my case is irrelevant as this command can be run from anywhere. This is the command i want to run:

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

Currently i have this :

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];

Any help will be appreciated.

Thanks,


回答1:


Here we go.

Working solution. 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];

Note: Place the proper xcode app name(/Applications/xcode 7.3) and simulator or device udid (if physical device).




回答2:


The launch path is the path to the command, and you seem to be be placing your command name after the '-t' in your arguments. Try something like this:

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];

I think NSTask places quotes around the arguments, but I'm not sure, so you may need to put escaped-quotes back around the "iPad…" argument.




回答3:


system function from stdlib will do what you want.

Just call it with the whole command as a parameter:

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>" ")


来源:https://stackoverflow.com/questions/26748038/how-to-run-a-command-in-terminal-from-my-cocoa-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!