简体   繁体   English

使用NSTask执行Shell命令-Objective-C可可

[英]Executing shell commands with NSTask - Objective-C Cocoa

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. 我一直在搜索几天和几个小时,我已经看到了很多这样的示例,但是无法弄清楚NSTask是如何工作的,比如说我想执行命令killall Dockdefaults write com.apple.Finder AppleShowAllFiles YES这样,我将如何去做。

I know how to execute an external shell script (sh) but need to be more sophisticated and use NSTask instead. 我知道如何执行外部Shell脚本(sh),但需要更加复杂,而应使用NSTask。

Thanks for any help!! 谢谢你的帮助!!

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. 您提供的启动路径和参数究竟由您要运行的命令及其参数决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM