19

I have been searching for days and hours for this, I have seen a lot of examples of this, but cannot figure out how NSTask works, let's say I wanted to execute the command killall Dock or defaults write com.apple.Finder AppleShowAllFiles YES something like that, how would I go about doing this.

I know how to execute an external shell script (sh) but need to be more sophisticated and use NSTask instead.

Thanks for any help!!

Rstew
  • 585
  • 2
  • 8
  • 21

1 Answers1

29

You could do something like:

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/bash"];
[task setArguments:@[ @"-c", @"/usr/bin/killall Dock" ]];
[task launch];

Exactly what launch path and arguments you provide are dictated by the command you want to run and its parameters.

Wain
  • 117,132
  • 14
  • 131
  • 151
  • Thanks for the reply!, how would I find out what launch path I need for different commands? – Rstew Jul 31 '13 at 18:25
  • 1
    My example cheats and runs a shell because it is often easier to supply the command and parameters that way. You can also pass the path to the unix executable Hyatt you want to run. – Wain Jul 31 '13 at 18:30
  • Thanks so much, you helped me out a lot! – Rstew Jul 31 '13 at 18:31