I am trying to test out running terminal commands through a macOS app. The command I am trying to run is "mkdir ~/Desktop/test" on a button click, but I think what I have so far is too simple to actually work. Or maybe it's a limitation of xCode not allowing access to my Desktop folder from the simulator?
Below is the code I have. The shell function is simply just a copy-paste of an answer I found here on StackOverflow:
struct ContentView: View {
@discardableResult func shell(_ command: String) -> (String?, Int32) {
let task = Process()
task.launchPath = "/bin/bash"
task.arguments = ["-c", command]
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)
task.waitUntilExit()
return (output, task.terminationStatus)
}
var body: some View {
VStack{
Button(action: {
self.shell("mkdir ~/Desktop/test")
}) {
Text("Run Command").font(.body)
}
}.frame(width:500,height:500) }
}
My ultimate goal is to try and run the screenshot command "cd ~/Desktop/ | screencapture test101.jpg". So if it's possible, would also love to know if i could pop this command into the shell function and also have it run.