Я новичок в разработке macos.
Я пытаюсь создать простое приложение macos в swift, которое использует скрипт bash для проверки, установлен ли пользователь Tor.
Следующий кодмой контроллер вида:
import Cocoa
class ViewController: NSViewController {
@IBOutlet var label: NSTextField!
@IBOutlet var spinner: NSProgressIndicator!
dynamic var isRunning = false
var outputPipe:Pipe!
var buildTask:Process!
override func viewDidLoad() {
super.viewDidLoad()
checkIfTorIsInstalled()
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
func checkIfTorIsInstalled() {
spinner.startAnimation(self)
runScript()
}
func runScript() {
isRunning = true
let taskQueue = DispatchQueue.global(qos: DispatchQoS.QoSClass.background)
taskQueue.async {
//1.
guard let path = Bundle.main.path(forResource: "CheckForTor", ofType: "command") else {
print("Unable to locate CheckForTor.command")
return
}
//2.
self.buildTask = Process()
self.buildTask.launchPath = path
//3.
self.buildTask.terminationHandler = {
task in
DispatchQueue.main.async(execute: {
self.spinner.alphaValue = 0
self.spinner.stopAnimation(self)
self.isRunning = false
})
}
self.captureStandardOutputAndRouteToTextView(self.buildTask)
//4.
self.buildTask.launch()
//5.
self.buildTask.waitUntilExit()
}
}
func captureStandardOutputAndRouteToTextView(_ task:Process) {
//1.
outputPipe = Pipe()
task.standardOutput = outputPipe
task.standardError = outputPipe
//2.
outputPipe.fileHandleForReading.waitForDataInBackgroundAndNotify()
//3.
NotificationCenter.default.addObserver(forName: NSNotification.Name.NSFileHandleDataAvailable, object: outputPipe.fileHandleForReading, queue: nil) {
notification in
//4.
let output = self.outputPipe.fileHandleForReading.availableData
let outputString = String(data: output, encoding: String.Encoding.utf8) ?? ""
//5.
DispatchQueue.main.async(execute: {
print("outputString = \(outputString)")
})
//6.
self.outputPipe.fileHandleForReading.waitForDataInBackgroundAndNotify()
}
}
}
Тогда у меня в основном пакете есть скрипт:
#!/bin/sh
# CheckForTor.command
# FullyNoded
#
# Created by Peter on 03/10/19.
# Copyright © 2019 Peter. All rights reserved.
tor --version
При запуске приложения возвращается следующая ошибка:
/Users/peter/Library/Developer/Xcode/DerivedData/FullyNoded-auhfxlayowyknmdyqlwszzkrnevb/Build/Products/Debug/FullyNoded.app/Contents/Resources/CheckForTor.command: line 9: tor: command not found
Тем не менее, когда я открываю свой терминал и запускаю tor --version
, я получаю правильный ответ. Как я могу запускать скрипты программно в приложении Macos Swift и получать те же функциональные возможности, что и при использовании терминала непосредственно на моем Mac?