Executing shell commands with NSTask - Objective-C Cocoa

心不动则不痛 提交于 2019-12-28 02:44:10

问题


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!!


回答1:


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.



来源:https://stackoverflow.com/questions/17976289/executing-shell-commands-with-nstask-objective-c-cocoa

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