gpt4 book ai didi

swift - 在 cocoa 应用程序中运行终端命令

转载 作者:行者123 更新时间:2023-11-28 05:40:52 25 4
gpt4 key购买 nike

我在 cocoa 应用程序上运行我的代码以运行一些命令行脚本时遇到问题

此功能在使用命令行工具时运行顺畅,但在使用带有一些 On Off UI 的完整 cocoa 应用程序时它根本不起作用

enter image description here我的脚本应该打开/关闭 http 和 https 代理

enter image description here

这是我的功能:

    private func runTask(_ cmd: String) {

// Create a Task instance
let task = Process()

// Set the task parameters
task.launchPath = "/bin/sh"
task.arguments = ["-c", String(format:"%@", cmd)]

// Create a Pipe and make the task
// put all the output there
let pipe = Pipe()
task.standardOutput = pipe

// Launch the task
task.launch()

// Get the data
let data = pipe.fileHandleForReading.readDataToEndOfFile()
guard let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { return }

print(output)
}

这是我的完整 ViewController 类:

import Cocoa

class ViewController: NSViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

@IBAction func onButtonTapped(_ sender: NSButton) {
print("onButtonTapped")
let selected: Switch = .on
let listOfNetworkCommands: String = [
#"networksetup -setwebproxystate "Wi-fi" \#(selected)"#, // switch http proxy
#"networksetup -setsecurewebproxystate "Wi-fi" \#(selected)"#, // switch https proxy
#"networksetup -setpassiveftp "Wi-fi" \#(selected)"# // switch passive ftp
].joined(separator: " && ")

runTask(listOfNetworkCommands)
}

@IBAction func offButtonTapped(_ sender: NSButton) {
print("onButtonTapped")
let selected: Switch = .off
let listOfNetworkCommands: String = [
#"networksetup -setwebproxystate "Wi-fi" \#(selected)"#, // switch http proxy
#"networksetup -setsecurewebproxystate "Wi-fi" \#(selected)"#, // switch https proxy
#"networksetup -setpassiveftp "Wi-fi" \#(selected)"# // switch passive ftp
].joined(separator: " && ")

runTask(listOfNetworkCommands)
}

enum Switch: String {
case on, off
}

private func runTask(_ cmd: String) {

// Create a Task instance
let task = Process()

// Set the task parameters
task.launchPath = "/bin/sh"
task.arguments = ["-c", String(format:"%@", cmd)]

// Create a Pipe and make the task
// put all the output there
let pipe = Pipe()
task.standardOutput = pipe

// Launch the task
task.launch()

// Get the data
let data = pipe.fileHandleForReading.readDataToEndOfFile()
guard let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { return }

print(output)
}

}

知道为什么我的函数没有在 cocoa 应用程序中触发吗?

最佳答案

通过在您的 Cocoa 应用程序中禁用 App Sandbox 找到简单的答案(在您的 Project app target > Capabilities 选项卡 > App Sandbox switch 下找到)。您会发现您被沙盒异常阻止了。禁用沙盒应该可以解决您的问题。

如果您过滤您的应用名称或沙盒进程,您也可以在 Console.app 中看到这一点。启用沙盒后,您可能会有这样的条目:

error 00:21:57.502273 +0000 sandboxd Sandbox: sh(17363) deny(1) file-read-data /dev/ttys003

关于swift - 在 cocoa 应用程序中运行终端命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56814198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com