Ashutosh
Ashutosh

Reputation: 5744

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

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,

Upvotes: 1

Views: 2031

Answers (3)

YSR fan
YSR fan

Reputation: 735

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

Upvotes: 1

RomanN
RomanN

Reputation: 2087

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

Upvotes: 0

John Cook
John Cook

Reputation: 117

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.

Upvotes: 0

Related Questions