问题描述
我一直在寻找这几天和几个小时,我已经看到了很多这样的例子,但不能弄清楚 NSTask 的工作原理,让我们说,我想执行命令 killall Dock
或默认写com.apple.Finder AppleShowAllFiles YES
这样的话,我该怎么做。
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.
我知道如何执行外部shell脚本(sh),但需要更复杂,并改用NSTask。
I know how to execute an external shell script (sh) but need to be more sophisticated and use NSTask instead.
推荐答案
您可以执行以下操作:
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.
这篇关于使用NSTask执行shell命令 - Objective-C Cocoa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!