简体   繁体   English

在可可应用程序中运行终端命令

[英]Running terminal commands in in cocoa app

I facing a problem when running my code on cocoa app to run some command line scripts我在可可应用程序上运行我的代码以运行一些命令行脚本时遇到问题

This function run smoothly when using Command line tool but when using full cocoa app with some On Off UI it not working at all使用命令行工具时,此功能运行流畅,但使用带有某些 On Off UI 的完整可可应用程序时,它根本无法运行

在此处输入图片说明 My script should turn on/off the http & https proxy我的脚本应该打开/关闭 http 和 https 代理

在此处输入图片说明

Here is my function:这是我的功能:

    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)
    }

And here is my full ViewController class:这是我完整的 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)
    }

}

Any idea why my function not triggered in the cocoa app?知道为什么我的功能没有在可可应用程序中触发吗?

Simple answer is found by disabling App Sandbox in your Cocoa Application (found under your Project app target > Capabilities tab > App Sandbox switch).通过在您的 Cocoa 应用程序中禁用 App Sandbox(在您的项目应用程序目标 > 功能选项卡 > App Sandbox 开关下找到)可以找到简单的答案。 You'll find that you're being blocked by a sandbox exception.您会发现您被沙箱异常阻止了。 Disabling sandboxing should fix your issue.禁用沙箱应该可以解决您的问题。

You can also see this in Console.app if you filter for your app name or the sandboxd process.如果您过滤应用程序名称或沙盒进程,您也可以在 Console.app 中看到这一点。 You'll likely have an entry like this when sandboxing is enabled:启用沙箱后,您可能会看到这样的条目:

error 00:21:57.502273 +0000 sandboxd Sandbox: sh(17363) deny(1) file-read-data /dev/ttys003错误 00:21:57.502273 +0000 沙箱沙箱:sh(17363) 拒绝 (1) 文件读取数据 /dev/ttys003

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

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