如何启动终端并以编程方式向其传递命令? |斯威夫特 3,macOS

How to launch terminal and pass it a command programatically? | Swift3, macOS

到目前为止我的代码如下所示:

import Foundation
import AppKit

print("Starting")
let base = NSWorkspace()

print("Launching Terminal")
base.launchApplication("Terminal") //launches terminal

print("Terminating terminal")
let apps = base.runningApplications
for app in apps {

    if app.localizedName == "Terminal" {
        print(app.localizedName)

        app.terminate() //Terminate
        break
    }
}

我已经想出了如何启动终端然后如何关闭它,但我不知道如何向它传递命令。 我要传递给它的命令是:"screencapture ~/Desktop/screenshot.jpg"

正如 Martin 所建议的,这会在 Swift 3

中执行 screencapture ~/Desktop/screenshot.jpg
let fileURL = try! FileManager.default.url(for: .desktopDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
Process.launchedProcess(launchPath: "/usr/sbin/screencapture", arguments: [fileURL.appendingPathComponent("screenshot.jpg").path])

获取 URL 到桌面文件夹的方法仅适用于非沙盒应用程序。