-2

I want be able to run this code through Swift for Terminal in macOS:

cd ~/Desktop/
mkdir "New Folder"

Basically I want to go to level of desktop then create a new Folder there. I want press a button in Swift/SwiftUI to run a function to execute the code for Terminal. Using Swift/SwiftUI is just for UI. I do not have shell file saved some where, all my codes is that I want be able to run those 2 commands for Terminal from Swift.

func runBashCode() {

    // need help here
    cd ~/Desktop/
    mkdir "New Folder"

}
ios coder
  • 1
  • 4
  • 31
  • 91
  • Why (use bash for this)? While not use `FileManager`? – MadProgrammer Jan 16 '22 at 02:57
  • I do not understand what you said, you mean that I should use bash? – ios coder Jan 16 '22 at 02:59
  • 1
    No, I mean why execute a shell command to create a directory when a API already exists to handle file level operations - [plenty of examples available](https://www.google.com/search?client=safari&rls=en&q=swift+make+directory&ie=UTF-8&oe=UTF-8) – MadProgrammer Jan 16 '22 at 03:00
  • Oh, that is the nature of question, as I mentioned in my question. @MadProgrammer – ios coder Jan 16 '22 at 03:01
  • @jnpdx: Are you a pro in bash? I am not, but I know enough that bash need a saved shell to works! Do you see a shell file or mentioning about a saved shell some where? – ios coder Jan 16 '22 at 03:03
  • @ioscoder I don't understand, sorry, but you want to create a directory? Why not use the `FileManager` API available in Swing? Why do you "need" to execute `mkdir` via the terminal directly? – MadProgrammer Jan 16 '22 at 03:03
  • @MadProgrammer: I do not understand your question about asking why either, I simply mentioned in my question that swift/swiftUI is JUST for UI, how ever you commented about using the native way of swift for making a directory! Am I missing something there to add to my question? – ios coder Jan 16 '22 at 03:05
  • @ioscoder [Execute shell/bash commands from Swift](https://gist.github.com/andreacipriani/8c3af3719da31c8fae2cdfa8c21e17ba). You will need to make use of `Process#currentDirectory` to "set the current directory" – MadProgrammer Jan 16 '22 at 03:06
  • I have dabbled in it and [this](https://github.com/x2on/OpenSSL-for-iPhone) does it. If you go though the code you’ll see the shell scripts and how they are run. Apple has shell scripts pretty locked down. You’ll have to play with submission. I remove the scripts prior to submission because it will fail. There may be a way to get them approved but I am not sure of the process. – lorem ipsum Jan 16 '22 at 03:07
  • @jnpdx: You can answer my question, I would accept it if I could run those commands. – ios coder Jan 16 '22 at 03:11

1 Answers1

0

try something like this:

Note: you need to delete the "App Sandbox" in the "Signing & Capabilities"

struct ContentView: View {
    let arg = NSHomeDirectory() + "/Desktop/newfolder"
    
    var body: some View {
        Button("mkdir") {
            runMkdir()
        }.frame(width: 333, height: 333)
    }
    
    func runMkdir() {
        let task = Process()
        task.executableURL = URL(fileURLWithPath: "/bin/mkdir")
        task.arguments = [arg]
        task.launch()
    }
    
}