1

Is there a way to programatically run OSX terminal commands from within an app.

For example, if I wanted to list all files in mac terminal I would use 'ls'. If I made an OSX app, is there a way I could run this command and then print out the result to the Xcode console?

I'm assuming this exists because it seems like something that should exist and with all the utilities on the App Store I'm sure some of them perform terminal commands.

Anyway, on short, how can I run terminal commands from an OSX app

Answers preferably in Swift but I can translate fairly well from Objective-C.

Thanks!

1 Answers1

1

What you need is a NSTask

This is the doc And this is just an example of execution

    NSString *path = @"/usr/local/bin/gls";
    NSArray *args = @[@"-l", @"-a", @"-F"];
    NSTask *task = [NSTask launchedTaskWithLaunchPath:path arguments:args];
    [task waitUntilExit];

If you also need to use the output of the command line tool you need to use NSPipe and you can find a nice tutorial here

IgnazioC
  • 4,554
  • 4
  • 33
  • 46