使用 NSTask 执行 shell 命令 - Objective-C Cocoa [英] Executing shell commands with NSTask - Objective-C Cocoa

查看:33
本文介绍了使用 NSTask 执行 shell 命令 - Objective-C Cocoa的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为此搜索了数天和数小时,我看过很多这样的例子,但无法弄清楚 NSTask 是如何工作的,假设我想执行命令 killall 码头defaults 写 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆